I\'m a bit new to malloc and C in general. I wanted to know how I can, if needed, extend the size of an otherwise fixed-size array with malloc.
Example:
Use realloc, but you have to allocate the array with malloc first. You're allocating it on the stack in the above example.
size_t myarray_size = 1000;
mystruct* myarray = malloc(myarray_size * sizeof(mystruct));
myarray_size += 1000;
mystruct* myrealloced_array = realloc(myarray, myarray_size * sizeof(mystruct));
if (myrealloced_array) {
myarray = myrealloced_array;
} else {
// deal with realloc failing because memory could not be allocated.
}