当前位置: 代码迷 >> C语言 >> [原创]求助,函数运行错误
  详细解决方案

[原创]求助,函数运行错误

热度:101   发布时间:2006-03-04 14:42:00.0
[原创]求助,函数运行错误
创建一个链表来存学生的学号和姓名
#include <stdio.h>
#include <stdlib.h>
struct student
{ char num[10];
char name[10];
struct student *next;
};
main()
{
int i,n;
struct student *head,*new,*p,*q;
head=NULL;
p=head;
printf("please input the length of link:\n");
scanf("%d",&n);
for(i=0;i<=n-1;i++) /*学号,姓名输入*/
{
new=(struct student *)malloc(sizeof(struct student));
printf("第%d个同学\n",i+1);
printf("please input the num:");
scanf("%s",new->num);
printf("please input the name:");
scanf("%s",new->name);
new->next=NULL;
if(head=NULL)
{
head=p=new;
}
else
{
p->next=new;
p=new;
}
}
q=head;
for(i=0;i<=n-1;i++) /*输出*/
{
if((q->next)!=NULL)
{
printf("第%d个同学\n",i+1);
printf("%s,%s\n",q->num,q->name);
}
}
}
运行到输入name时就有错误,内存不能written
搜索更多相关的解决方案: 运行  函数  

----------------解决方案--------------------------------------------------------
if(head=NULL)

----------------解决方案--------------------------------------------------------
用结构数组更好
----------------解决方案--------------------------------------------------------
知道了,THANKS
----------------解决方案--------------------------------------------------------
  相关解决方案