写代码不掉头发. 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 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教