I'm trying to create an array of a struct but I don't know it size at the beginning.
struct MyStruct** thing;
size_t thing_len = 0;
thing = (struct MyStruct**) malloc(thing_len * sizeof(struct MyStruct*));
//...
thing_len += 1
thing = (struct MyStruct**) realloc(thing_len * sizeof(struct MyStruct*));
When I do that thing
gets the type MyStruct*
instead of MyStruct**
and contains 0x0
. But when I do
struct MyStruct* thing;
size_t thing_len = 0;
thing = malloc(thing_len * sizeof(struct MyStruct));
//...
thing_len += 1
thing = realloc(thing_len * sizeof(struct MyStruct));
It works!!
I don't know if it changes something but I am using -ansi
and -pedantic
options.