【发布时间】:2021-08-13 22:39:43
【问题描述】:
动态内存取初始大小输入,填满后自动增加,然后输入新数据。 如果你检查数组中的值,会有奇怪的值。
int main() {
BOOK *books = NULL;
int count = 0;
int size = -1;
menu(books, size, count);
return 0;
}
void menu(BOOK *books, int size, int count) {
printf("input initial size.\n");
scanf("%d", &size);
while (getchar() != '\n');
books = allocArray(size);
if (books == NULL) {
return;
}
while (1) {
printf("========menu========\n");
printf("1. input book data\n");
printf("2. books\n");
int selector = -1;
scanf("%d", &selector);
switch (selector) {
case 1:
inputBook(books, &count, &size);
break;
case 2:
showAllBooks(books, count);
break;
default:
free(books);
books = NULL;
return;
}
}
}
void inputBook(BOOK *books, int *count, int *size) {
if (*count == *size) {
printf("--------realloc +5\n");
*size = *size * 5;
books = reAllocArray(books, *size);
}
printf("--------input--------\n");
while (getchar() != '\n');
printf("title :");
scanf("%s", books[*count].title);
printf("author :");
scanf("%s", books[*count].author);
printf("price :");
scanf("%d", &books[*count].price);
*count = *count + 1;
}
BOOK *allocArray(int size) {
BOOK *books = NULL;
books = (BOOK *) malloc(sizeof(BOOK) * size);
if (books == NULL) {
printf("malloc fail\n");
} else {
memset(books, 0, sizeof(BOOK) * size);
}
return books;
}
BOOK *reAllocArray(BOOK *books, int size) {
BOOK *temp = (BOOK *) realloc(books, sizeof(BOOK) * size);
if (temp == NULL) {
printf("realloc fail\n");
}
return temp;
}
void showAllBooks(BOOK *books, int count) {
for (int i = 0; i < count; ++i) {
printf("%d. title: %s author: %s proce: %d\n", i + 1, books[i].title, books[i].author, books[i].price);
}
}
当初始大小设置为1时,则输入1、1、1个数据,并输入附加数据。 realloc 发生,我输入 2, 2, 2 如果检查数组,它包含 1,1,1 和 null、null 和 0。
如果之前输入了更多数据,现有数据也可能会损坏。
【问题讨论】:
-
inputBook在本地更改books参数,但调用者看不到更改。您需要返回新值(如reAllocArray)或使用指向指针的指针。 -
菜单有选择1和2。开关有case 1和5
-
@IanAbbott 谢谢,返回了新值,所以效果很好。
-
这些教程链接可能有助于解释按值传递函数参数和按引用传递函数参数之间的区别:Call by ValueCall by Reference
-
@AndreasWenzel 非常感谢,很有帮助。