[原创]求助,函数运行错误
创建一个链表来存学生的学号和姓名#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