鱼C论坛

 找回密码
 立即注册
查看: 1153|回复: 4

[已解决]请问各位这段代码是哪里出问题了呀,出现段错误的提示

[复制链接]
发表于 2020-9-13 13:23:11 | 显示全部楼层 |阅读模式

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

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

x
  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[20];
  12.     char author[10];
  13.     float price;
  14.     struct Date date;
  15.     char publisher[20];
  16. };

  17. void getInput(struct Book* book)
  18. {
  19.     int count = 1;

  20.     printf("Enter the %d's book name\n", count);
  21.     scanf("%s", book->title);
  22.     printf("Enter the author name\n");
  23.     scanf("%s", book->author);
  24.     printf("Enter the price\n");
  25.     scanf("%f", &book->price);
  26.     printf("Enter the date\n");
  27.     scanf("%d %d %d", &book->date.year, &book->date.month, &book->date.day);
  28.     printf("Enter the publisher\n");
  29.     scanf("%s", book->publisher);
  30. }

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

  39. void getSize(struct Book* book)
  40. {
  41.     book = (struct Book*)malloc(sizeof(struct Book));
  42.     if (book == NULL)
  43.     {
  44.         printf("Error\n");
  45.         exit(1);
  46.     }
  47. }

  48. void freeLibrary(struct Book* book)
  49. {
  50.     free(book);
  51. }

  52. void main()
  53. {
  54.     int n;
  55.     printf("How many books do you want to save?\n");
  56.     scanf("%d", &n);
  57.     struct Book* library[n];

  58.     printf("Now start save book\n");
  59.     for (int i = 0; i < n; i++)
  60.     {
  61.         getSize(library[i]);
  62.         getInput(library[i]);
  63.     }

  64.     printf("Now list the information about all book\n");
  65.     for (int i = 0; i < n; i++)
  66.     {

  67.         printf("This is the %d's book\n", i + 1);
  68.         printBook(library[i]);
  69.     }

  70.     for (int i = 0; i < n; i++)
  71.     {
  72.         freeLibrary(library[i]);
  73.     }
  74. }

复制代码
最佳答案
2020-9-13 14:39:39
本帖最后由 sunrise085 于 2020-9-13 14:40 编辑

你在main中定义的是结构体指针数组,然后给指针分配空间,这样是有问题的,调用函数getSize动态分配给指针空间,函数结束后,就无效了。

应该是定义结构体数组,然后给数组内的每个元素动态分配空间
最后释放的时候先释放后面的元素
  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[20];
  12.     char author[10];
  13.     float price;
  14.     struct Date date;
  15.     char publisher[20];
  16. };

  17. void getInput(struct Book* book)
  18. {
  19.     int count = 1;

  20.     printf("Enter the %d's book name\n", count);
  21.     scanf("%s", book->title);
  22.     printf("Enter the author name\n");
  23.     scanf("%s", book->author);
  24.     printf("Enter the price\n");
  25.     scanf("%f", &book->price);
  26.     printf("Enter the date\n");
  27.     scanf("%d %d %d", &book->date.year, &book->date.month, &book->date.day);
  28.     printf("Enter the publisher\n");
  29.     scanf("%s", book->publisher);
  30. }

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

  39. void getSize(struct Book* book)
  40. {
  41.     book = (struct Book*)malloc(sizeof(struct Book));
  42.     if (book == NULL)
  43.     {
  44.         printf("Error\n");
  45.         exit(1);
  46.     }
  47. }

  48. void freeLibrary(struct Book* book)
  49. {
  50.     free(book);
  51. }

  52. void main()
  53. {
  54.     int n;
  55.     printf("How many books do you want to save?\n");
  56.     scanf("%d", &n);
  57.     struct Book library[n];  //这里定义成结构体数组

  58.     printf("Now start save book\n");
  59.     for (int i = 0; i < n; i++)
  60.     {
  61.         getSize(&library[i]);//这里实参为地址,下面几个函数调用时,实参也一样
  62.         getInput(&library[i]);
  63.     }

  64.     printf("Now list the information about all book\n");
  65.     for (int i = 0; i < n; i++)
  66.     {

  67.         printf("This is the %d's book\n", i + 1);
  68.         printBook(&library[i]);
  69.     }

  70.     for (int i = n; i >0; i++)
  71.     {
  72.         freeLibrary(&library[i]);
  73.     }
  74. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-13 13:44:47 | 显示全部楼层
你可以把那个i 放前边和n一块定义了应该就能运行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-13 14:08:57 | 显示全部楼层
一枚丶学渣 发表于 2020-9-13 13:44
你可以把那个i 放前边和n一块定义了应该就能运行了

能运行,而且也不报错,可是运行到输入作者时就提示段错误,核心转存&#128579;
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-13 14:39:39 | 显示全部楼层    本楼为最佳答案   
本帖最后由 sunrise085 于 2020-9-13 14:40 编辑

你在main中定义的是结构体指针数组,然后给指针分配空间,这样是有问题的,调用函数getSize动态分配给指针空间,函数结束后,就无效了。

应该是定义结构体数组,然后给数组内的每个元素动态分配空间
最后释放的时候先释放后面的元素
  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[20];
  12.     char author[10];
  13.     float price;
  14.     struct Date date;
  15.     char publisher[20];
  16. };

  17. void getInput(struct Book* book)
  18. {
  19.     int count = 1;

  20.     printf("Enter the %d's book name\n", count);
  21.     scanf("%s", book->title);
  22.     printf("Enter the author name\n");
  23.     scanf("%s", book->author);
  24.     printf("Enter the price\n");
  25.     scanf("%f", &book->price);
  26.     printf("Enter the date\n");
  27.     scanf("%d %d %d", &book->date.year, &book->date.month, &book->date.day);
  28.     printf("Enter the publisher\n");
  29.     scanf("%s", book->publisher);
  30. }

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

  39. void getSize(struct Book* book)
  40. {
  41.     book = (struct Book*)malloc(sizeof(struct Book));
  42.     if (book == NULL)
  43.     {
  44.         printf("Error\n");
  45.         exit(1);
  46.     }
  47. }

  48. void freeLibrary(struct Book* book)
  49. {
  50.     free(book);
  51. }

  52. void main()
  53. {
  54.     int n;
  55.     printf("How many books do you want to save?\n");
  56.     scanf("%d", &n);
  57.     struct Book library[n];  //这里定义成结构体数组

  58.     printf("Now start save book\n");
  59.     for (int i = 0; i < n; i++)
  60.     {
  61.         getSize(&library[i]);//这里实参为地址,下面几个函数调用时,实参也一样
  62.         getInput(&library[i]);
  63.     }

  64.     printf("Now list the information about all book\n");
  65.     for (int i = 0; i < n; i++)
  66.     {

  67.         printf("This is the %d's book\n", i + 1);
  68.         printBook(&library[i]);
  69.     }

  70.     for (int i = n; i >0; i++)
  71.     {
  72.         freeLibrary(&library[i]);
  73.     }
  74. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-13 16:03:40 | 显示全部楼层
sunrise085 发表于 2020-9-13 14:39
你在main中定义的是结构体指针数组,然后给指针分配空间,这样是有问题的,调用函数getSize动态分配给指针 ...

谢谢老哥
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 19:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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