鱼C论坛

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

[已解决]为什么这个在储存第二个人的数据时会报错啊??

[复制链接]
发表于 2023-8-12 13:10:28 | 显示全部楼层 |阅读模式

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

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

x
为什么这个在储存第二个人的数据时会报错啊??


  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>

  4. #define LEN sizeof(struct cesh)

  5. struct cesh* creat();
  6. void print(struct cesh*head);
  7. int i;

  8. void main()
  9. {
  10.         struct cesh* stu;
  11.         stu = creat();
  12.         print(stu);

  13.         system("pause");
  14. }

  15. struct cesh
  16. {
  17.         int xuehao;
  18.         double score;
  19.         struct cesh* next;
  20. };

  21. struct cesh* creat()
  22. {
  23.         struct cesh *p1,*p2,*head;
  24.         p1 = p2 = (struct cesh*)malloc(LEN);
  25.         printf("输入学号。\n");
  26.         scanf_s("%d", &p1->xuehao);
  27.         printf("输入成绩。\n");
  28.         scanf_s("%lf", &p1->score);
  29.         head = NULL;
  30.         i = 0;
  31.         while (p1->xuehao)
  32.         {
  33.                 i++;
  34.                 if (i == 1)
  35.                 {
  36.                         head = p1;
  37.                 }

  38.                 else
  39.                 {
  40.                         p2->next = p1;
  41.                 }
  42.                 p2 = p1;
  43.                 p1 = (struct cesh*)malloc(LEN);
  44.                 printf("输入学号\n");
  45.                 scanf_s("%d",p1->xuehao);
  46.                 printf("输入成绩\n");
  47.                 scanf_s("%lf",p1->score);
  48.         }
  49.         p2->next = NULL;
  50.         return head;
  51. }

  52. void print(struct cesh* head)
  53. {
  54.         struct cesh* p;
  55.         printf("\n这里一共有 %d 个数据\n\n", i);
  56.         p = head;
  57.         if (head)
  58.         {
  59.                 do
  60.                 {
  61.                         printf("学号为 %d 的成绩为 %0.2f.\n", p->xuehao, p->score);
  62.                         p = p->next;
  63.                 } while (p);
  64.         }
  65. }
复制代码
最佳答案
2023-8-12 13:10:36
问题出在以下这行代码:
  1. scanf_s(\%d\, p1->xuehao);
复制代码

以及
  1. scanf_s(\%lf\, p1->score);
复制代码

在这里,你应该传递变量的地址给scanf_s函数,而不是变量的值。所以需要修改为:
  1. scanf_s(\%d\, &(p1->xuehao));
复制代码

以及
  1. scanf_s(\%lf\, &(p1->score));
复制代码

修改后的完整代码如下所示:
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #define LEN sizeof(struct cesh)
  5. struct cesh* creat();
  6. void print(struct cesh* head);
  7. int i;
  8. int main() {
  9.     struct cesh* stu;
  10.     stu = creat();
  11.     print(stu);
  12.     system(\pause\);
  13.     return 0;
  14. }
  15. struct cesh {
  16.     int xuehao;
  17.     double score;
  18.     struct cesh* next;
  19. };
  20. struct cesh* creat() {
  21.     struct cesh* p1, * p2, * head;
  22.     p1 = p2 = (struct cesh*)malloc(LEN);
  23.     printf(\输入学号。\
  24. \);
  25.     scanf_s(\%d\, &(p1->xuehao));
  26.     printf(\输入成绩。\
  27. \);
  28.     scanf_s(\%lf\, &(p1->score));
  29.     head = NULL;
  30.     i = 0;
  31.     while (p1->xuehao) {
  32.         i++;
  33.         if (i == 1) {
  34.             head = p1;
  35.         }
  36.         else {
  37.             p2->next = p1;
  38.         }
  39.         p2 = p1;
  40.         p1 = (struct cesh*)malloc(LEN);
  41.         printf(\输入学号\
  42. \);
  43.         scanf_s(\%d\, &(p1->xuehao));
  44.         printf(\输入成绩\
  45. \);
  46.         scanf_s(\%lf\, &(p1->score));
  47.     }
  48.     p2->next = NULL;
  49.     return head;
  50. }
  51. void print(struct cesh* head) {
  52.     struct cesh* p;
  53.     printf(\\
  54. 这里一共有 %d 个数据\
  55. \
  56. \, i);
  57.     p = head;
  58.     if (head) {
  59.         do {
  60.             printf(\学号为 %d 的成绩为 %0.2f.\
  61. \, p->xuehao, p->score);
  62.             p = p->next;
  63.         } while (p);
  64.     }
  65. }
复制代码

这样修改后,你应该能够正确地储存第二个人的数据了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-12 13:10:36 | 显示全部楼层    本楼为最佳答案   
问题出在以下这行代码:
  1. scanf_s(\%d\, p1->xuehao);
复制代码

以及
  1. scanf_s(\%lf\, p1->score);
复制代码

在这里,你应该传递变量的地址给scanf_s函数,而不是变量的值。所以需要修改为:
  1. scanf_s(\%d\, &(p1->xuehao));
复制代码

以及
  1. scanf_s(\%lf\, &(p1->score));
复制代码

修改后的完整代码如下所示:
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #define LEN sizeof(struct cesh)
  5. struct cesh* creat();
  6. void print(struct cesh* head);
  7. int i;
  8. int main() {
  9.     struct cesh* stu;
  10.     stu = creat();
  11.     print(stu);
  12.     system(\pause\);
  13.     return 0;
  14. }
  15. struct cesh {
  16.     int xuehao;
  17.     double score;
  18.     struct cesh* next;
  19. };
  20. struct cesh* creat() {
  21.     struct cesh* p1, * p2, * head;
  22.     p1 = p2 = (struct cesh*)malloc(LEN);
  23.     printf(\输入学号。\
  24. \);
  25.     scanf_s(\%d\, &(p1->xuehao));
  26.     printf(\输入成绩。\
  27. \);
  28.     scanf_s(\%lf\, &(p1->score));
  29.     head = NULL;
  30.     i = 0;
  31.     while (p1->xuehao) {
  32.         i++;
  33.         if (i == 1) {
  34.             head = p1;
  35.         }
  36.         else {
  37.             p2->next = p1;
  38.         }
  39.         p2 = p1;
  40.         p1 = (struct cesh*)malloc(LEN);
  41.         printf(\输入学号\
  42. \);
  43.         scanf_s(\%d\, &(p1->xuehao));
  44.         printf(\输入成绩\
  45. \);
  46.         scanf_s(\%lf\, &(p1->score));
  47.     }
  48.     p2->next = NULL;
  49.     return head;
  50. }
  51. void print(struct cesh* head) {
  52.     struct cesh* p;
  53.     printf(\\
  54. 这里一共有 %d 个数据\
  55. \
  56. \, i);
  57.     p = head;
  58.     if (head) {
  59.         do {
  60.             printf(\学号为 %d 的成绩为 %0.2f.\
  61. \, p->xuehao, p->score);
  62.             p = p->next;
  63.         } while (p);
  64.     }
  65. }
复制代码

这样修改后,你应该能够正确地储存第二个人的数据了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 13:11:14 | 显示全部楼层
这段代码在储存第二个人的数据时会报错是因为在输入学号和成绩时,使用了错误的scanf_s函数的调用方式。

scanf_s函数需要传入参数的地址,而不是参数本身。所以在以下两行代码中:

scanf_s("%d",p1->xuehao);
scanf_s("%lf",p1->score);

应该改为:

scanf_s("%d", &p1->xuehao);
scanf_s("%lf", &p1->score);

修改后的代码如下:

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>

  4. #define LEN sizeof(struct cesh)

  5. struct cesh* creat();
  6. void print(struct cesh*head);
  7. int i;

  8. void main()
  9. {
  10.     struct cesh* stu;
  11.     stu = creat();
  12.     print(stu);

  13.     system("pause");
  14. }

  15. struct cesh
  16. {
  17.     int xuehao;
  18.     double score;
  19.     struct cesh* next;
  20. };

  21. struct cesh* creat()
  22. {
  23.     struct cesh *p1,*p2,*head;
  24.     p1 = p2 = (struct cesh*)malloc(LEN);
  25.     printf("输入学号。\n");
  26.     scanf_s("%d", &p1->xuehao);
  27.     printf("输入成绩。\n");
  28.     scanf_s("%lf", &p1->score);
  29.     head = NULL;
  30.     i = 0;
  31.     while (p1->xuehao)
  32.     {
  33.         i++;
  34.         if (i == 1)
  35.         {
  36.             head = p1;
  37.         }
  38.         else
  39.         {
  40.             p2->next = p1;
  41.         }
  42.         p2 = p1;
  43.         p1 = (struct cesh*)malloc(LEN);
  44.         printf("输入学号\n");
  45.         scanf_s("%d", &p1->xuehao);
  46.         printf("输入成绩\n");
  47.         scanf_s("%lf", &p1->score);
  48.     }
  49.     p2->next = NULL;
  50.     return head;
  51. }

  52. void print(struct cesh* head)
  53. {
  54.     struct cesh* p;
  55.     printf("\n这里一共有 %d 个数据\n\n", i);
  56.     p = head;
  57.     if (head)
  58.     {
  59.         do
  60.         {
  61.             printf("学号为 %d 的成绩为 %0.2f.\n", p->xuehao, p->score);
  62.             p = p->next;
  63.         } while (p);
  64.     }
  65. }
复制代码


这样修改后,代码应该能够正常运行并储存第二个人的数据。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 13:11:27 | 显示全部楼层
根据您提供的代码,我发现在存储第二个人的数据时会报错的原因是在两处输入语句中存在错误。以下是您的代码中需要进行修正的地方:

1. 在 creat() 函数中,下面这两行代码中的 scanf_s 函数调用不正确:

  
  1.    printf("输入学号\n");
  2.    scanf_s("%d", p1->xuehao);
  3.    printf("输入成绩\n");
  4.    scanf_s("%lf", p1->score);
  5.    
复制代码


   应将这两行代码修改为:

  
  1.    printf("输入学号\n");
  2.    scanf_s("%d", &(p1->xuehao));
  3.    printf("输入成绩\n");
  4.    scanf_s("%lf", &(p1->score));
  5.    
复制代码


   scanf_s 函数需要传递变量的地址作为参数,而 p1->xuehaop1->score 分别是变量 xuehaoscore 的值,所以需要使用 & 运算符获取它们的地址。

2. 在 main() 函数中,void main() 应改为 int main(),并在函数的末尾添加 return 0;

修改后的代码如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define LEN sizeof(struct cesh)

  4. struct cesh {
  5.     int xuehao;
  6.     double score;
  7.     struct cesh* next;
  8. };

  9. struct cesh* creat();
  10. void print(struct cesh* head);

  11. int i;

  12. int main() {
  13.     struct cesh* stu;
  14.     stu = creat();
  15.     print(stu);
  16.     system("pause");
  17.     return 0;
  18. }

  19. struct cesh* creat() {
  20.     struct cesh* p1, * p2, * head;
  21.     p1 = p2 = (struct cesh*)malloc(LEN);
  22.     printf("输入学号。\n");
  23.     scanf_s("%d", &(p1->xuehao));
  24.     printf("输入成绩。\n");
  25.     scanf_s("%lf", &(p1->score));
  26.     head = NULL;
  27.     i = 0;
  28.     while (p1->xuehao) {
  29.         i++;
  30.         if (i == 1) {
  31.             head = p1;
  32.         }
  33.         else {
  34.             p2->next = p1;
  35.         }
  36.         p2 = p1;
  37.         p1 = (struct cesh*)malloc(LEN);
  38.         printf("输入学号\n");
  39.         scanf_s("%d", &(p1->xuehao));
  40.         printf("输入成绩\n");
  41.         scanf_s("%lf", &(p1->score));
  42.     }
  43.     p2->next = NULL;
  44.     return head;
  45. }

  46. void print(struct cesh* head) {
  47.     struct cesh* p;
  48.     printf("\n这里一共有 %d 个数据\n\n", i);
  49.     p = head;
  50.     if (head) {
  51.         do {
  52.             printf("学号为 %d 的成绩为 %0.2f.\n", p->xuehao, p->score);
  53.             p = p->next;
  54.         } while (p);
  55.     }
  56. }
复制代码


修改后的代码应该可以正常地存储多个人的数据并输出结果。如果您遇到其他问题,请随时提问。
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 18:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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