Name: Anonymous 2009-10-08 11:02
Hey /prog/riders,
I'm fairly new to C, I'm working with a piece of code that reads from a file to populate an array. My problem is the size of the array is not static, and I have no idea how large the end result is going to be.
Here's something I hacked up for an example, I haven't started working on the piece of code yet, I'm just wondering how I should handle the memory allocation for stct.
I'm fairly new to C, I'm working with a piece of code that reads from a file to populate an array. My problem is the size of the array is not static, and I have no idea how large the end result is going to be.
Here's something I hacked up for an example, I haven't started working on the piece of code yet, I'm just wondering how I should handle the memory allocation for stct.
struct strct{
bool b;
char c;
}stct*;
int stct_length, stct_height, x, y;
char s[1024];
file = fopen(path, "rb");
for(y = 0;!feof(file);++y){
fscanf(file, "%s", &s);
for(x = 0;;++x){
t = y * stct_length + x;
switch(s[x]){
case '0':
stct[y * stct_length + x].b = 0;
stct[y * stct_length + x].c = 'd';
continue;
case '1':
stct[y * stct_length + x].b = 1;
stct[y * stct_length + x].c = 'f';
continue;
case '\n':
break;
}
break;
}
if(y == 0){
stct_length = x;
}
else if(x != stct_length){
printf("line %d is of irregular length", y)
break;
}
}
stct_height = y;