写代码不掉头发. 2021-05-13 13:07 采纳率: 80%
浏览 22
已结题

Dev正常运行 VS报错 求解答

#include<stdio.h>
#include<stdlib.h>
struct student
{
	int num;
	float score;
	struct student* next;
};
void print(struct student* head)
{

	while (head != NULL)
	{
		printf("num:%d,score:%.2f\n", head->num, head->score);
		head = head->next;
	}
}
struct student* create()
{
	struct student* head = NULL;
	struct student* pnew, * pold;
	int num;
	float score;
	scanf_s("%d %f", &num, &score);
	if (num == 0 && score == 0)
	{
		printf("Empty!\n");
		return head;
	}
	do
	{
		pnew = (struct student*)malloc(sizeof(struct student));
		pnew->num = num;
		pnew->score = score;
		if (head == NULL)
		{
			head = pnew;
			pnew->next = NULL;
			pold = head;
		}
		else
		{
			pnew->next = pold->next;
			pold->next = pnew;
			pold = pnew;
		}
		scanf_s("%d %f", &num, &score);
	} while (num != 0 && score != 0);
	printf("Scores are:\n");
	return head;
}

int main()
{
	struct student* head;
	head = create();
	print(head);
	return 0;
}

同样的代码在Dev可以正常运行 在VS却显示如上的错误 求解答

  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2021-05-13 13:14
    关注

    pold没有绝对赋值的地方。如果先执行else处理,那么第43行使用pold->next就会崩溃。编译器是这么想的,所以你要初始化pold

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月21日
  • 已采纳回答 3月13日

悬赏问题

  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题