本人菜鸟,请教VC中的一个问题
下面程序哪里写错了,请高人指出,谢谢啦:
#include "stdafx.h"
#include"string.h"
#include"stdlib.h"
#include"stdio.h"
struct stud_nod{
int num;
char name[20];
int score;
struct stud_nod *next;
};
struct stud_nod *creat()
{
struct stud_nod *head,*tail,*p;
int num,score;
char name[20];
int size=sizeof(struct stud_nod);
head=tail=NULL;
scanf("%d%s%d",&num,name,&score);
while(num!=0)
{
p=(struct stud_nod*)malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d%s%d",&num,name,&score);
}
return head;
}
int main()
{
struct stud_nod *head;
head=creat();
while(head!=NULL)
{
printf("%8d %20s %6d\n",head->num,head->name,head->score);
head=head->next;
}
}
运行后出现以下错误信息:
![](https://www.wenjiangs.com/wp-content/uploads/chinaunix/202207/none.gif)
#include "stdafx.h"
#include"string.h"
#include"stdlib.h"
#include"stdio.h"
struct stud_nod{
int num;
char name[20];
int score;
struct stud_nod *next;
};
struct stud_nod *creat()
{
struct stud_nod *head,*tail,*p;
int num,score;
char name[20];
int size=sizeof(struct stud_nod);
head=tail=NULL;
scanf("%d%s%d",&num,name,&score);
while(num!=0)
{
p=(struct stud_nod*)malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d%s%d",&num,name,&score);
}
return head;
}
int main()
{
struct stud_nod *head;
head=creat();
while(head!=NULL)
{
printf("%8d %20s %6d\n",head->num,head->name,head->score);
head=head->next;
}
}
运行后出现以下错误信息:
![](https://www.wenjiangs.com/wp-content/uploads/chinaunix/202207/none.gif)
错误信息.jpg (24.28 KB, 下载次数: 1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
错误信息.jpg (24.28 KB, 下载次数: 1)
下载附件
2011-05-01 21:54 上传
错误信息.jpg (24.28 KB, 下载次数: 1)
下载附件
2011-05-01 21:53 上传
p->score=score;
下面加上
p->next=NULL;
太感谢了,不出错了。请问为什么在p->score=score;下面加上p->next=NULL; 就好了,能说明一下么?