简单的动态链表错误
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alloc.h>
struct student
{
int num;
char name[30];
struct student *next;
};
int main()
{
struct student *head;
head=(struct student*)malloc(sizeof(struct student));
head->next=NULL;
create(head);
display(head);
system("pause");
return 0;
}
void create(struct student*p)//创建函数
{
struct student*q;
int num=1;
char name[30];
q=p;
while(1)
{
printf("请输入学号:");
scanf("%d",&num);
if(num!=0)
{
printf("\n请输入姓名");
scanf("%s",name);
strcpy(p->name,name);
p->num=num;
q=(struct student*)malloc(sizeof(struct student));
p->next=q;
}
else
{
p->next=NULL;
break;
}
}
}
void display(struct student*p)//输出函数
{
while(p!=NULL)
{
printf("%d%s",p->num,p->name);
p=p->next;
}
}
编译通过,但是运行到输出这一步就出错,这是为什么呢???
[解决办法]
main()中的create(head)也要改成create(&head);