I would like to know how to allocate sequential memory for an array of structures inside of another structure. Say I have struct1 which has an array of struct2, I would like to have it all in one sequential block of memory.
I could use malloc to allocate the block, but how would I assign the array’s memory?
struct1 *set = malloc(sizeof(struct1) + sizeof(struct2)*number_of_structs);
set->arr = (struct2*)set + sizeof(struct); //This is probably wrong
set->arr[0].data = 1;
set->arr[1].data = 2;
...
Thank you.
Use a flexible array member :