私はこの構造体の配列を持っていますが、この関数は配列のポインタへのポインタをとります。元のサイズは2です。したがって、サイズに達するたびに、再割り当てしてサイズを倍増する必要があります。このコードが実行されると、reallocから無効な古いサイズのエラーが発生します。私は間違って何をしていますか?あなたの関数で構造体のポインタへのポインタを持っているとき、どのようにreallocを使いますか?
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;
}
while(fscanf(...)!= EOF) 'は安全ではありません。 *コンバージョンの*いくつか*が*すべて*ではない場合に何が起こるかを考えてみてください。 – EOF