Accessing a Flexible array Member in C -


i have struct looks this:

typedef struct testcase testcase; typedef struct testcase {        char * username;     testcase *c[];    // flexible array member } testcase; 

and in file i'm trying set flexible array member null, doesn't seem work (i'm not allowed change how it's been defined)

void readin(file * file, testcase ** t) {     *t = malloc(sizeof(testcase));      (*t)->c = null; //gives error  } 

i'm using double pointers because that's what's been specified me (this isn't entire code, snipit). (as there code later free variables allocated).

any appreciated.

take @ line:

 (*t)->c = null; 

this tries assign null array, isn't allowed. writing this:

int array[137]; array = null; // not allowed 

when have flexible array member, intent when malloc memory object, overallocate memory need make room array elements. example, code

*t = malloc(sizeof(testcase) + numarrayelems * sizeof(testcase*)); 

will allocate new testcase has array of numarrayelems pointers.

if don't know in advance how many pointers you'll need, should switch away using flexible array members , use normal pointers.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -