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

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -