鱼C论坛

 找回密码
 立即注册
查看: 3495|回复: 7

[已解决]为啥VC6.0可以,Visual Studio 2019就报错呢?

[复制链接]
发表于 2019-8-6 11:22:17 | 显示全部楼层 |阅读模式

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

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

x
  1. /*
  2. 要求:1.设计一个程序,用链表储存学生信息,并以成绩进行排序输出;
  3.       2.学生信息用结构体进行封装;
  4. */

  5. #include<stdio.h>
  6. #include<stdlib.h>

  7. struct STUDENT//学生信息结构体
  8. {
  9.         char name[20];
  10.         int age;
  11.         float score;
  12.         char num[20];
  13. };

  14. struct NODE
  15. {
  16.         struct STUDENT data;//使用结构体封装
  17.         struct NODE* next;
  18. };

  19. struct NODE* CreateLink(void);//创建链表函数声明
  20. void lnitLink(struct NODE *);//链表结点信息输入函数声明
  21. void BubbleSort(struct NODE *);//链表结点排序函数声明
  22. void OutputLink(struct NODE *);//链表结点信息输出函数声明

  23. int main(void)
  24. {
  25.         struct NODE * head =CreateLink();//创建链表
  26.         lnitLink(head);//链表初始化
  27.         BubbleSort(head);//冒泡排序
  28.         printf("\n*************************排序后的结果如下:******************************\n\n");
  29.         OutputLink(head);//排序结果输出
  30.         return 0;
  31. }

  32. //****************以下为创建链表函数***********************************
  33. struct NODE* CreateLink(void)
  34. {
  35.         int i = 0;
  36.         int cnt = 0;
  37.         struct NODE * head = malloc(sizeof * head);
  38.         struct NODE * move = head;
  39.         move->next = NULL;
  40.         printf("请输入学生的数量:");
  41.         scanf("%d",&cnt);
  42.         getchar();

  43.         for (i = 0; i < cnt; ++i)
  44.         {
  45.                 struct NODE* fresh = malloc(sizeof * fresh);
  46.                 move->next = fresh;
  47.                 fresh->next = NULL;
  48.                 move = fresh;
  49.         }
  50.         return head;
  51. }

  52. //****************以下为链表信息输入函数***********************************
  53. void lnitLink(struct NODE *head)
  54. {
  55.         int i = 1;
  56.         struct NODE* move = head->next;
  57.         while (NULL != move)
  58.         {
  59.                 printf("\n请输入第%d个学生的姓名、年龄、成绩、学号:\n\n", i);
  60.                 scanf("%s%d%f%s",move->data.name,&move->data.age,&move->data.score,move->data.num);
  61.                 getchar();
  62.                 move = move->next;
  63.                 ++i;
  64.         }
  65.         return;
  66. }

  67. //******************以下为冒泡排序函数************************************************
  68. void BubbleSort(struct NODE* head)
  69. {
  70.         struct NODE* move = head->next;
  71.         struct NODE* turn = head->next;
  72.         struct STUDENT buf;
  73.         if (NULL == move)
  74.         {
  75.                 printf("链表为空\n");
  76.                 exit(0);//退出程序
  77.         }
  78.         while (NULL != turn->next)
  79.         {
  80.                 while (NULL != move->next)
  81.                 {
  82.                         if (move->data.score < move->next->data.score)
  83.                         {
  84.                                 buf = move->data;
  85.                                 move->data = move->next->data;
  86.                                 move->next->data =buf;
  87.                         }
  88.                         move = move->next;
  89.                 }
  90.                 move = head->next;
  91.                 turn = turn->next;
  92.         }
  93.         return;
  94. }


  95. //*******************以下为链表输出函数***************************************

  96. void OutputLink(struct NODE* head)
  97. {
  98.         struct NODE* move = head->next;//初始化为首结点
  99.         while (NULL != move)
  100.         {
  101.                 printf("[姓名:%s 年龄:%d 成绩:%.1f 学号:%s]\n\n",move->data.name,move->data.age,move->data.score,move->data.num);
  102.                 move = move->next;
  103.         }
  104.         return;
  105. }
复制代码


这段代码在VC6.0里运行正常,Visual Studio 2019就报错。这是为什么呢?


                               
登录/注册后可看大图
最佳答案
2019-8-6 13:10:10
本帖最后由 Neverturnback 于 2019-8-6 13:13 编辑

创建项目的时候把安全检测关掉
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-6 12:02:35 | 显示全部楼层
本帖最后由 chxchxkkk 于 2019-8-6 12:06 编辑

语句格式不一样吧。
printf是c的格式,
c++ 是 cout << xxx   << endl; 这样的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-6 12:32:42 | 显示全部楼层
编译器不一样肯定会这样的  不过如果你用的语法符合C标准的化  ,vs2019能编译通过   简单设置一下就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-6 13:10:10 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Neverturnback 于 2019-8-6 13:13 编辑

创建项目的时候把安全检测关掉
安全检查.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-6 13:22:53 | 显示全部楼层
本帖最后由 Neverturnback 于 2019-8-6 13:24 编辑
  1. /*
  2. 要求:1.设计一个程序,用链表储存学生信息,并以成绩进行排序输出;
  3. 2.学生信息用结构体进行封装;
  4. */

  5. #include<stdio.h>
  6. #include<stdlib.h>

  7. struct STUDENT//学生信息结构体
  8. {
  9.         char name[20];
  10.         int age;
  11.         float score;
  12.         char num[20];
  13. };

  14. struct NODE
  15. {
  16.         struct STUDENT data;//使用结构体封装
  17.         struct NODE* next;
  18. };

  19. struct NODE* CreateLink(void);//创建链表函数声明
  20. void lnitLink(struct NODE *);//链表结点信息输入函数声明
  21. void BubbleSort(struct NODE *);//链表结点排序函数声明
  22. void OutputLink(struct NODE *);//链表结点信息输出函数声明

  23. int main(void)
  24. {
  25.         struct NODE * head = CreateLink();//创建链表
  26.         lnitLink(head);//链表初始化
  27.         BubbleSort(head);//冒泡排序
  28.         printf("\n*************************排序后的结果如下:******************************\n\n");
  29.         OutputLink(head);//排序结果输出
  30.         return 0;
  31. }

  32. //****************以下为创建链表函数***********************************
  33. struct NODE* CreateLink(void)
  34. {
  35.         int i = 0;
  36.         int cnt = 0;
  37.         struct NODE * head = (struct NODE *)malloc(sizeof(struct NODE));
  38.         struct NODE * move = head;
  39.         move->next = NULL;
  40.         printf("请输入学生的数量:");
  41.         scanf("%d", &cnt);
  42.         getchar();

  43.         for (i = 0; i < cnt; ++i)
  44.         {
  45.                 struct NODE* fresh = (struct NODE *)malloc(sizeof(struct NODE));
  46.                 move->next = fresh;
  47.                 fresh->next = NULL;
  48.                 move = fresh;
  49.         }
  50.         return head;
  51. }

  52. //****************以下为链表信息输入函数***********************************
  53. void lnitLink(struct NODE *head)
  54. {
  55.         int i = 1;
  56.         struct NODE* move = head->next;
  57.         while (NULL != move)
  58.         {
  59.                 printf("\n请输入第%d个学生的姓名、年龄、成绩、学号:\n\n", i);
  60.                 scanf("%s%d%f%s", move->data.name, &move->data.age, &move->data.score, move->data.num);
  61.                 getchar();
  62.                 move = move->next;
  63.                 ++i;
  64.         }
  65.         return;
  66. }

  67. //******************以下为冒泡排序函数************************************************
  68. void BubbleSort(struct NODE* head)
  69. {
  70.         struct NODE* move = head->next;
  71.         struct NODE* turn = head->next;
  72.         struct STUDENT buf;
  73.         if (NULL == move)
  74.         {
  75.                 printf("链表为空\n");
  76.                 exit(0);//退出程序
  77.         }
  78.         while (NULL != turn->next)
  79.         {
  80.                 while (NULL != move->next)
  81.                 {
  82.                         if (move->data.score < move->next->data.score)
  83.                         {
  84.                                 buf = move->data;
  85.                                 move->data = move->next->data;
  86.                                 move->next->data = buf;
  87.                         }
  88.                         move = move->next;
  89.                 }
  90.                 move = head->next;
  91.                 turn = turn->next;
  92.         }
  93.         return;
  94. }


  95. //*******************以下为链表输出函数***************************************

  96. void OutputLink(struct NODE* head)
  97. {
  98.         struct NODE* move = head->next;//初始化为首结点
  99.         while (NULL != move)
  100.         {
  101.                 printf("[姓名:%s 年龄:%d 成绩:%.1f 学号:%s]\n\n", move->data.name, move->data.age, move->data.score, move->data.num);
  102.                 move = move->next;
  103.         }

  104.         getchar();
  105.         getchar();
  106.         return;
  107. }
复制代码


你所有的malloc的部分都出错了,帮你修改过了,还有malloc分配的空间最后记得free释放,释放我没帮你写,你自己加把
运行结果.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-8-6 21:08:59 | 显示全部楼层
路过……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-8-8 14:46:48 | 显示全部楼层
Neverturnback 发表于 2019-8-6 13:10
创建项目的时候把安全检测关掉

2019版本创建项目时没有这个选项,在创建后项目属性里修改的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-10 07:43:17 | 显示全部楼层
Neverturnback 发表于 2019-8-6 13:22
你所有的malloc的部分都出错了,帮你修改过了,还有malloc分配的空间最后记得free释放,释放我没帮你写 ...

请问,如何释放这段代码中malloc申请的空间?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-16 10:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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