c - Is it possible to create a struct whose size is not known at compile time? -


as question states looking create struct in c total size not know @ compile time.

for example, create struct contains count value , array count elements. know implemented as:

typedef struct mystruct{     int count;     int *myarray; } mystruct; 

however, want struct take 1 solid block of memory use memcpy() on @ later point in time. this:

typedef struct mystruct{     int count;     int myarray[count]; } mystruct; 

it sounds you're looking flexible array members:

typedef struct mystruct {     int count;     int myarray[]; } mystruct; 

then, when allocate later:

mystruct *x = malloc(sizeof(mystruct) + n * sizeof(int)); x->count = n; 

Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -