I have this array of structs and this function takes a pointer to the pointer of the array. The original size is 2, so whenever it reaches the size, I need to realloc and double the size. When this code runs, I get an invalid old size error from the realloc. What am I doing wrong?
int PopulateArray(struct Inventory **inv, int *size, FILE *inputFile) {
int count = 0;
printf("address: %u\n", inv);
printf("address: %u\n", **inv);
int itemNumber;
int quantity;
float price;
int month;
int year;
while (fscanf(inputFile, "%i %i %f %i/%i", &itemNumber,
&quantity, &price, &month, &year) != EOF) {
(*inv)->itemNumber = itemNumber;
(*inv)->quantity = quantity;
(*inv)->price = price;
(*inv)->expDate.month = month;
(*inv)->expDate.year = year;
printf("count: %i size: %i\n", count, *size);
if (count == *size - 1) {
inv = realloc(inv, (*size * 2 * sizeof(struct Inventory)));
*size *= 2;
}
inv++;
count++;
}
return count;
}