Wednesday, 11 September 2013

Malloc with a structure containing variably sized objects

Malloc with a structure containing variably sized objects

I'm trying to create a structure which contains information from each line
in a file, so the size of the structure is dependent on the length of the
file. C doesn't like me doing,
int makeStruct(int x){
typedef struct
{
int a[x], b[x];
char c[x], d[x];
char string[100][x];
} agentInfo;
return 0;
}
I know I have to Malloc, but I'm not sure what. Do I have to Malloc the
structure and the arrays inside of it? I don't know how I'd Malloc the
entire struct as I won't know how big it will be until I know x, so I
can't use size-of? Any help appreciated.

No comments:

Post a Comment