|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <stdlib.h>
- struct Date
- {
- int year;
- int month;
- int day;
- };
- struct Book
- {
- char title[20];
- char author[10];
- float price;
- struct Date date;
- char publisher[20];
- };
- void getInput(struct Book* book)
- {
- int count = 1;
- printf("Enter the %d's book name\n", count);
- scanf("%s", book->title);
- printf("Enter the author name\n");
- scanf("%s", book->author);
- printf("Enter the price\n");
- scanf("%f", &book->price);
- printf("Enter the date\n");
- scanf("%d %d %d", &book->date.year, &book->date.month, &book->date.day);
- printf("Enter the publisher\n");
- scanf("%s", book->publisher);
- }
- void printBook(struct Book* book)
- {
- printf("Book name: %s\n", book->title);
- printf("Book author: %s\n", book->author);
- printf("Book price: %.2f\n", book->price);
- printf("Book date: %d-%d-%d", book->date.year, book->date.month, book->date.day);
- printf("Book publisher: %s", book->publisher);
- }
- void getSize(struct Book* book)
- {
- book = (struct Book*)malloc(sizeof(struct Book));
- if (book == NULL)
- {
- printf("Error\n");
- exit(1);
- }
- }
- void freeLibrary(struct Book* book)
- {
- free(book);
- }
- void main()
- {
- int n;
- printf("How many books do you want to save?\n");
- scanf("%d", &n);
- struct Book* library[n];
- printf("Now start save book\n");
- for (int i = 0; i < n; i++)
- {
- getSize(library[i]);
- getInput(library[i]);
- }
- printf("Now list the information about all book\n");
- for (int i = 0; i < n; i++)
- {
- printf("This is the %d's book\n", i + 1);
- printBook(library[i]);
- }
- for (int i = 0; i < n; i++)
- {
- freeLibrary(library[i]);
- }
- }
复制代码
本帖最后由 sunrise085 于 2020-9-13 14:40 编辑
你在main中定义的是结构体指针数组,然后给指针分配空间,这样是有问题的,调用函数getSize动态分配给指针空间,函数结束后,就无效了。
应该是定义结构体数组,然后给数组内的每个元素动态分配空间
最后释放的时候先释放后面的元素
- #include <stdio.h>
- #include <stdlib.h>
- struct Date
- {
- int year;
- int month;
- int day;
- };
- struct Book
- {
- char title[20];
- char author[10];
- float price;
- struct Date date;
- char publisher[20];
- };
- void getInput(struct Book* book)
- {
- int count = 1;
- printf("Enter the %d's book name\n", count);
- scanf("%s", book->title);
- printf("Enter the author name\n");
- scanf("%s", book->author);
- printf("Enter the price\n");
- scanf("%f", &book->price);
- printf("Enter the date\n");
- scanf("%d %d %d", &book->date.year, &book->date.month, &book->date.day);
- printf("Enter the publisher\n");
- scanf("%s", book->publisher);
- }
- void printBook(struct Book* book)
- {
- printf("Book name: %s\n", book->title);
- printf("Book author: %s\n", book->author);
- printf("Book price: %.2f\n", book->price);
- printf("Book date: %d-%d-%d", book->date.year, book->date.month, book->date.day);
- printf("Book publisher: %s", book->publisher);
- }
- void getSize(struct Book* book)
- {
- book = (struct Book*)malloc(sizeof(struct Book));
- if (book == NULL)
- {
- printf("Error\n");
- exit(1);
- }
- }
- void freeLibrary(struct Book* book)
- {
- free(book);
- }
- void main()
- {
- int n;
- printf("How many books do you want to save?\n");
- scanf("%d", &n);
- struct Book library[n]; //这里定义成结构体数组
- printf("Now start save book\n");
- for (int i = 0; i < n; i++)
- {
- getSize(&library[i]);//这里实参为地址,下面几个函数调用时,实参也一样
- getInput(&library[i]);
- }
- printf("Now list the information about all book\n");
- for (int i = 0; i < n; i++)
- {
- printf("This is the %d's book\n", i + 1);
- printBook(&library[i]);
- }
- for (int i = n; i >0; i++)
- {
- freeLibrary(&library[i]);
- }
- }
复制代码
|
|