I have the following code.:
struct O1 {
int a;
int b;
int c;
};
struct O2 {
int f;
int g;
struct O1 *array; //pointer to an array of structs O1
};
struct O2 *list = malloc(25 * sizeof(struct O1)); //array of 25 structs O2
struct O1 *innerlist = malloc(sizeof(struct O1)); //array of structs O1 contained inside each struct O2
innerlist[0].a = 1;
innerlist[0].b = 2;
innerlist[0].c = 3;
(*list)[0].f = 1;
(*list)[0].g = 1;
(*list)[0].array = &innerlist[0]; //filling the parameters via some loop for example
So my question is, did I allocate the arrays correctly?
For the inner structure contained inside each struct O2, I only wanted an array with just one element (one struct O1) that I could then expand further via realloc for example but I tried storing more struct O1s in struct O2 and it works so there must have been more memory allocated somewhere even though I did not multiply the sizeof by anything when creating the array of struct O1.
I also have a question, how do I free the arrays?
I guess I must first free the inner array first but I am having trouble here.
I have created a function for freeing the memory that takes the array of struct O2 as a parameter.
But when I type free(list->obj); it seems to be freeing the obj of the first struct O2 in the list and free(list[n]->obj) does not seem to be working.
struct 01will not compile. Did you intend capital-Ostruct O1? There are some missing;too after thestructdefinitions. Is this your code?(*list)[0]is syntactically equivalent to*((*list)+0). This is wrong I think. You said your example compiles. Can you post that example please?