When you update inv from realloc() , your inv now points to the beginning of the newly modified array. So your code
if (count == *size - 1) { inv = realloc(inv, (*size * 2 * sizeof(struct Inventory*))); *size *= 2; } inv++;
the last inv++ will make inv efficiently for inv[1] , not inv[count] , which you probably would like to specify.
I add below because incorrect answers are supported
Sentence
*inv = realloc(*inv, (*size * 2 * sizeof(struct Inventory)));
wrong.
What you are trying to do is double the array of pointers dynamically. So the correct pointer type for going to realloc here struct Inventory ** .
(You probably created the initial table pptr = malloc(sizeof(struct Inventory*) * INIT_SIZE) , so inv correct type for realloc here)
Having said that after realloc executed in your function, the original inv pointer used by the code that calls this function is no longer valid, so when you return this function, you lose the pointer to resize the array. To handle this, you must return the new value of the inv pointer of the calling function.
additional editing:
And don't forget to allocate memory for the actual struct Inventory element:
inv[count] = malloc(sizeof(struct Inventory));
at the beginning of the cycle.
source share