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
Post a Comment