Resizing an array with C

Start off by creating the array:

structName ** sarray = (structName **) malloc(0 * sizeof(structName *));

Always keep track of the size separately:

size_t sarray_len = 0;

To increase or truncate:

sarray = (structName **) realloc(sarray, (sarray_len + offset) * sizeof(structName *));

Then set the size:

sarray_len += offset;

Happy to help and hope that helps.

Leave a Comment