|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 AliceJames 于 2021-4-10 15:04 编辑
目标:创建一个结构体指针数组作为图书馆,让用户可以录入书籍信息并且选择任意一本书打印其信息
我用一个数组来存放结构体,可以正常运行 - #include <stdio.h>
- #include <stdlib.h>
- struct Date
- {
- int year;
- int month;
- int day;
- };
- struct Book
- {
- char title[128];
- char author[40];
- float price;
- struct Date date;
- char publisher[40];
- };
- void getInput(struct Book *);
- void printBook(struct Book *);
- //录入一本书的书籍信息
- void getInput(struct Book *book)
- {
- printf("title: ");
- scanf("%s", book->title);
- printf("author: ");
- scanf("%s", book->author);
- printf("price: ");
- scanf("%f", &book->price);
- printf("date: ");
- scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
- printf("publisher: ");
- scanf("%s", book->publisher);
- }
- //打印一本书的书籍信息
- void printBook(struct Book *book)
- {
- printf("title: %s\n", book->title);
- printf("author: %s\n", book->author);
- printf("price: %.2f\n", book->price);
- printf("date: %u\n", book->date.year, book->date.month, book->date.day);
- printf("publisher: %s\n", book->publisher);
- }
- int main(void)
- {
- int count = 1, zhiling, temp, i;
- struct Book *b = NULL;//将b定义为结构体指针
- b = (struct Book *)malloc(sizeof(struct Book));
- if(b == NULL)
- {
- printf("Failed!\n");
- exit(1);
- }
- //先录入第一本书
- printf("Write the first book!\n");
- getInput(&b[0]);
- do
- {
- //让用户输入指令
- printf("Enter an integer:(1 write a book/ 2 seek a book/ 0 leave): ");
- scanf("%d", &zhiling);
- switch(zhiling)
- {
- case 0:{
- free(b);
- printf("Thanks for use!\n");
- return 0;
- }break;
- case 1:{
- count++;
- b = (struct Book *)realloc(b, count * sizeof(struct Book));//希望通过用户输入来改变数组大小,而不是由用户输入一个值来确定数组元素个数
- if(b == NULL)
- {
- printf("Failed!\n");
- exit(1);
- }
- printf("This is No.%d book!\n", count);
- getInput(&b[count - 1]);
- }break;
- case 2:{
- //打印已录入书籍的书名,以供用户选择查看哪本书
- for(i = 0; i < count; i++)
- {
- printf("No.%d book: %s\n", i + 1, b[i].title);
- }
- printf("Which one do you want?");
- scanf("%d", &temp);
- printBook(&b[temp - 1]);
- }break;
- }
- putchar('\n');
- }while(1);
- return 0;
- }
复制代码
将数组元素换成结构体指针,在调用getInput函数时出现段错误(Segmentation fault)。。。请问如何解决? - #include <stdio.h>
- #include <stdlib.h>
- struct Date
- {
- int year;
- int month;
- int day;
- };
- struct Book
- {
- char title[128];
- char author[40];
- float price;
- struct Date date;
- char publisher[40];
- };
- void getInput(struct Book *);
- void printBook(struct Book *);
- void getInput(struct Book *book)
- {
- printf("title: ");
- scanf("%s", book->title);
- printf("author: ");
- scanf("%s", book->author);
- printf("price: ");
- scanf("%f", &book->price);
- printf("date: ");
- scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
- printf("publisher: ");
- scanf("%s", book->publisher);
- }
- void printBook(struct Book *book)
- {
- printf("title: %s\n", book->title);
- printf("author: %s\n", book->author);
- printf("price: %.2f\n", book->price);
- printf("date: %u\n", book->date.year, book->date.month, book->date.day);
- printf("publisher: %s\n", book->publisher);
- }
- int main(void)
- {
- int count = 1, zhiling, temp, i;
- struct Book **b = NULL;//将b定义为指向结构体指针数组的指针
- b = (struct Book **)malloc(sizeof(struct Book *));
- if(b == NULL)
- {
- printf("Failed!\n");
- exit(1);
- }
- printf("Write the first book!\n");
- getInput(b[0]);
- do
- {
- printf("Enter an integer:(1 write a book/ 2 seek a book/ 0 leave): ");
- scanf("%d", &zhiling);
- switch(zhiling)
- {
- case 0:{
- free(b);
- printf("Thanks for use!\n");
- return 0;
- }break;
- case 1:{
- count++;
- b = (struct Book **)realloc(b, count * sizeof(struct Book *));//希望通过用户输入来改变数组大小,而不是由用户输入一个值来确定数组元素个数
- if(b == NULL)
- {
- printf("Failed!\n");
- exit(1);
- }
- printf("This is No.%d book!\n", count);
- getInput(b[count - 1]);
- }break;
- case 2:{
- //打印已录入书籍的书名,以供用户选择查看哪本书
- for(i = 0; i < count; i++)
- {
- printf("No.%d book: %s\n", i + 1, b[i]->title);
- }
- printf("Which one do you want?");
- scanf("%d", &temp);
- printBook(b[temp - 1]);
- }break;
- }
- putchar('\n');
- }while(1);
- return 0;
- }
复制代码 |
|