#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
struct student
{
char name[10];
struct student *next;
};
void list(struct student *p)
{
while(1)
{
printf("%s\n", p->name);
if(p->next != NULL)
{
p = p->next ;
}
else
{
break;
}
}
}
struct student *create()
{
struct student *head;
struct student s1 = { "11", NULL };
struct student s2 = { "11", NULL };
struct student s3 = { "22", NULL };
struct student s4 = { "33", NULL };
s1.next = & s2;
s2.next = & s3;
s3.next = & s4;
head = & s1;
return head;
}
int main()
{
struct student *p;
p = create();
list(p);
return 0;
}
为啥无法真常输出?该咋改,求帮助
create函数里定义了多个局部变量,出了这个函数就被回收了。这几个学生变量要用malloc申请动态空间才行