书上列子有问题,帮忙修改一下
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define N 10
typedef struct
{ char name[20];
struct node *link;
}STUD;
STUD*creat(int n)
{STUD*p,*h,*s;
int i;
if((h=(STUD*)malloc(sizeof(STUD)))==NULL)
{printf("没有足够的内存空间分配!");
exit(0);
}
scanf("%s",h->name);
h->name=NULL;
p=h;
for (i=1;i<n;i++)
{if((s=(stud*)malloc(sizeof(stud)))==NULL)
{printf("不能分配内存空间!");
exit(0);
}
p->link=s;
printf("请输入第%d个人的姓名",i+1);
scanf("%s",s->name);
s->link=NULL;
p=s;
}
return(h);
}
main()
{int number;
STUD*head=NULL;
number=N;
head=creat(number);
}
--------------------Configuration: helloc - Win32 Debug--------------------
Compiling...
hello.c
D:\VC\helloc\hello.c(14) : warning C4013: 'exit' undefined; assuming extern returning int
D:\VC\helloc\hello.c(17) : error C2106: '=' : left operand must be l-value
D:\VC\helloc\hello.c(20) : error C2065: 'stud' : undeclared identifier
D:\VC\helloc\hello.c(20) : error C2059: syntax error : ')'
D:\VC\helloc\hello.c(24) : warning C4133: '=' : incompatible types - from 'struct STUD *' to 'struct node *'
D:\VC\helloc\hello.c(30) : error C2059: syntax error : 'return'
D:\VC\helloc\hello.c(31) : error C2059: syntax error : '}'
执行 cl.exe 时出错.
helloc.exe - 1 error(s), 0 warning(s)
[解决办法]
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#define N 10
typedef struct tagSTUD
{ char name[20];
struct tagSTUD*link;
}STUD;
STUD*creat(int n)
{STUD*p,*h,*s;
int i;
if((h=(STUD*)malloc(sizeof(STUD)))==NULL)
{printf("没有足够的内存空间分配!");
exit(0);
}
scanf("%s",h->name);
h->link=NULL;
p=h;
for (i=1;i<n;i++)
{if((s=(STUD*)malloc(sizeof(STUD)))==NULL)
{printf("不能分配内存空间!");
exit(0);
}
p->link=s;
printf("请输入第%d个人的姓名",i+1);
scanf("%s",s->name);
s->link=NULL;
p=s;
}
return(h);
}
main()
{int number;
STUD*head=NULL;
number=N;
head=creat(number);
}