鱼C论坛

 找回密码
 立即注册
查看: 3568|回复: 3

结构体指针数组构建图书馆

[复制链接]
发表于 2021-4-10 14:50:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 AliceJames 于 2021-4-10 15:04 编辑

目标:创建一个结构体指针数组作为图书馆,让用户可以录入书籍信息并且选择任意一本书打印其信息

我用一个数组来存放结构体,可以正常运行
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Date
  4. {
  5.         int year;
  6.         int month;
  7.         int day;
  8. };

  9. struct Book
  10. {
  11.         char title[128];
  12.         char author[40];
  13.         float price;
  14.         struct Date date;
  15.         char publisher[40];
  16. };

  17. void getInput(struct Book *);
  18. void printBook(struct Book *);

  19. //录入一本书的书籍信息
  20. void getInput(struct Book *book)
  21. {
  22.         printf("title: ");
  23.         scanf("%s", book->title);
  24.         printf("author: ");
  25.         scanf("%s", book->author);
  26.         printf("price: ");
  27.         scanf("%f", &book->price);
  28.         printf("date: ");
  29.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  30.         printf("publisher: ");
  31.         scanf("%s", book->publisher);
  32. }

  33. //打印一本书的书籍信息
  34. void printBook(struct Book *book)
  35. {
  36.         printf("title: %s\n", book->title);
  37.         printf("author: %s\n", book->author);
  38.         printf("price: %.2f\n", book->price);
  39.         printf("date: %u\n", book->date.year, book->date.month, book->date.day);
  40.         printf("publisher: %s\n", book->publisher);
  41. }

  42. int main(void)
  43. {
  44.         int count = 1, zhiling, temp, i;
  45.         struct Book *b = NULL;//将b定义为结构体指针

  46.         b = (struct Book *)malloc(sizeof(struct Book));
  47.         if(b == NULL)
  48.         {
  49.                 printf("Failed!\n");
  50.                 exit(1);
  51.         }

  52.         //先录入第一本书
  53.         printf("Write the first book!\n");
  54.         getInput(&b[0]);

  55.         do
  56.         {
  57.                 //让用户输入指令
  58.                 printf("Enter an integer:(1 write a book/ 2 seek a book/ 0 leave): ");
  59.                 scanf("%d", &zhiling);

  60.                 switch(zhiling)
  61.                 {
  62.                         case 0:{
  63.                                 free(b);
  64.                                 printf("Thanks for use!\n");
  65.                                 return 0;
  66.                         }break;

  67.                         case 1:{
  68.                                 count++;
  69.                                 b = (struct Book *)realloc(b, count * sizeof(struct Book));//希望通过用户输入来改变数组大小,而不是由用户输入一个值来确定数组元素个数
  70.                                 if(b == NULL)
  71.                                 {
  72.                                         printf("Failed!\n");
  73.                                         exit(1);
  74.                                 }

  75.                                 printf("This is No.%d book!\n", count);
  76.                                 getInput(&b[count - 1]);
  77.                         }break;

  78.                         case 2:{
  79.                                 //打印已录入书籍的书名,以供用户选择查看哪本书
  80.                                 for(i = 0; i < count; i++)
  81.                                 {
  82.                                         printf("No.%d book: %s\n", i + 1, b[i].title);
  83.                                 }
  84.                                 printf("Which one do you want?");
  85.                                 scanf("%d", &temp);

  86.                                 printBook(&b[temp - 1]);
  87.                         }break;
  88.                 }
  89.                 putchar('\n');
  90.         }while(1);

  91.         return 0;
  92. }
复制代码


将数组元素换成结构体指针,在调用getInput函数时出现段错误(Segmentation fault)。。。请问如何解决?
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Date
  4. {
  5.         int year;
  6.         int month;
  7.         int day;
  8. };

  9. struct Book
  10. {
  11.         char title[128];
  12.         char author[40];
  13.         float price;
  14.         struct Date date;
  15.         char publisher[40];
  16. };

  17. void getInput(struct Book *);
  18. void printBook(struct Book *);

  19. void getInput(struct Book *book)
  20. {
  21.         printf("title: ");
  22.         scanf("%s", book->title);
  23.         printf("author: ");
  24.         scanf("%s", book->author);
  25.         printf("price: ");
  26.         scanf("%f", &book->price);
  27.         printf("date: ");
  28.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  29.         printf("publisher: ");
  30.         scanf("%s", book->publisher);
  31. }

  32. void printBook(struct Book *book)
  33. {
  34.         printf("title: %s\n", book->title);
  35.         printf("author: %s\n", book->author);
  36.         printf("price: %.2f\n", book->price);
  37.         printf("date: %u\n", book->date.year, book->date.month, book->date.day);
  38.         printf("publisher: %s\n", book->publisher);
  39. }

  40. int main(void)
  41. {
  42.         int count = 1, zhiling, temp, i;
  43.         struct Book **b = NULL;//将b定义为指向结构体指针数组的指针

  44.         b = (struct Book **)malloc(sizeof(struct Book *));
  45.         if(b == NULL)
  46.         {
  47.                 printf("Failed!\n");
  48.                 exit(1);
  49.         }

  50.         printf("Write the first book!\n");
  51.         getInput(b[0]);

  52.         do
  53.         {
  54.                 printf("Enter an integer:(1 write a book/ 2 seek a book/ 0 leave): ");
  55.                 scanf("%d", &zhiling);

  56.                 switch(zhiling)
  57.                 {
  58.                         case 0:{
  59.                                 free(b);
  60.                                 printf("Thanks for use!\n");
  61.                                 return 0;
  62.                         }break;

  63.                         case 1:{
  64.                                 count++;
  65.                                 b = (struct Book **)realloc(b, count * sizeof(struct Book *));//希望通过用户输入来改变数组大小,而不是由用户输入一个值来确定数组元素个数
  66.                                 if(b == NULL)
  67.                                 {
  68.                                         printf("Failed!\n");
  69.                                         exit(1);
  70.                                 }

  71.                                 printf("This is No.%d book!\n", count);
  72.                                 getInput(b[count - 1]);
  73.                         }break;

  74.                         case 2:{
  75.                                 //打印已录入书籍的书名,以供用户选择查看哪本书
  76.                                 for(i = 0; i < count; i++)
  77.                                 {
  78.                                         printf("No.%d book: %s\n", i + 1, b[i]->title);
  79.                                 }
  80.                                 printf("Which one do you want?");
  81.                                 scanf("%d", &temp);

  82.                                 printBook(b[temp - 1]);
  83.                         }break;
  84.                 }
  85.                 putchar('\n');
  86.         }while(1);

  87.         return 0;
  88. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-4-11 00:28:31 | 显示全部楼层
void getInput(struct Book *);
void printBook(struct Book *);
换成
void getInput(struct Book **);
void printBook(struct Book **);
试试看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-11 15:29:53 | 显示全部楼层
chxchxkkk 发表于 2021-4-11 00:28
void getInput(struct Book *);
void printBook(struct Book *);
换成

改了之后还是段错误
是我改的不对吗
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Date
  4. {
  5.         int year;
  6.         int month;
  7.         int day;
  8. };

  9. struct Book
  10. {
  11.         char title[128];
  12.         char author[40];
  13.         float price;
  14.         struct Date date;
  15.         char publisher[40];
  16. };

  17. void getInput(struct Book **);
  18. void printBook(struct Book **);

  19. void getInput(struct Book **book)//改
  20. {
  21.         printf("title: ");
  22.         scanf("%s", (*book)->title);
  23.         printf("author: ");
  24.         scanf("%s", (*book)->author);
  25.         printf("price: ");
  26.         scanf("%f", &(*book)->price);
  27.         printf("date: ");
  28.         scanf("%d-%d-%d", &(*book)->date.year, &(*book)->date.month, &(*book)->date.day);
  29.         printf("publisher: ");
  30.         scanf("%s", (*book)->publisher);
  31. }

  32. void printBook(struct Book **book)//改
  33. {
  34.         printf("title: %s\n", (*book)->title);
  35.         printf("author: %s\n", (*book)->author);
  36.         printf("price: %.2f\n", (*book)->price);
  37.         printf("date: %u\n", (*book)->date.year, (*book)->date.month, (*book)->date.day);
  38.         printf("publisher: %s\n", (*book)->publisher);
  39. }

  40. int main(void)
  41. {
  42.         int count = 1, zhiling, temp, i;
  43.         struct Book **b = NULL;//将b定义为指向结构体指针数组的指针

  44.         b = (struct Book **)malloc(sizeof(struct Book *));
  45.         if(b == NULL)
  46.         {
  47.                 printf("Failed!\n");
  48.                 exit(1);
  49.         }

  50.         printf("Write the first book!\n");
  51.         getInput(&b[0]);//改

  52.         do
  53.         {
  54.                 printf("Enter an integer:(1 write a book/ 2 seek a book/ 0 leave): ");
  55.                 scanf("%d", &zhiling);

  56.                 switch(zhiling)
  57.                 {
  58.                         case 0:{
  59.                                 free(b);
  60.                                 printf("Thanks for use!\n");
  61.                                 return 0;
  62.                         }break;

  63.                         case 1:{
  64.                                 count++;
  65.                                 b = (struct Book **)realloc(b, count * sizeof(struct Book *));//希望通过用户输入来改变数组大小,而不是由用户输入一个值来确定数组元素个数
  66.                                 if(b == NULL)
  67.                                 {
  68.                                         printf("Failed!\n");
  69.                                         exit(1);
  70.                                 }

  71.                                 printf("This is No.%d book!\n", count);
  72.                                 getInput(&b[count - 1]);//改
  73.                         }break;

  74.                         case 2:{
  75.                                 //打印已录入书籍的书名,以供用户选择查看哪本书
  76.                                 for(i = 0; i < count; i++)
  77.                                 {
  78.                                         printf("No.%d book: %s\n", i + 1, b[i]->title);
  79.                                 }
  80.                                 printf("Which one do you want?");
  81.                                 scanf("%d", &temp);

  82.                                 printBook(&b[temp - 1]);//改
  83.                         }break;
  84.                 }
  85.                 putchar('\n');
  86.         }while(1);

  87.         return 0;
  88. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-14 00:32:51 | 显示全部楼层
你全程没有给结构体指针赋相应的结构体空间,肯定会出现段错误啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-8 01:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表