李宁nba代言人:帮我看看这段C代码

来源:百度文库 编辑:高考问答 时间:2024/04/28 10:43:39
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
typedef struct student
{
long num;
float score;
struct student *next;
};
int n;
struct sutdent *creat()
{
struct student *head;
struct sutdent *p1,*p2;
n=0;
p1=p2=(struct student *) malloc(LEN);
head = NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void main()
{
int *p;
*p=creat();
}
很多错误还有警告
但不知道怎么弄

1、有两个student写成了sutdent
2、creat返回的是student类型的指针,但你声明的p是int型指针。
应该写student *p=NULL;p=creat();
3、还有你根本没有用到给student取别名,typedef去掉也无所谓。

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
typedef struct student
{
long num;
float score;
struct student *next;
};
int n;
struct sutdent *creat() //sutdent?
{
struct student *head;
struct sutdent *p1,*p2;
n=0;
p1=p2=(struct student *) malloc(LEN);
head = NULL;
while(p1->num!=0) //p1->num不可预料
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void main()
{
int *p;
*p=creat(); //error类型不相符
}
//////////////////
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
typedef struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat()
{
struct student *head=NULL;
struct student *p1=NULL;
struct student *p2=NULL;
n=0;
p1=(struct student *) malloc(LEN);
p2=p1;
scanf("%ld,%f",&p1->num,&p1->score);
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void main()
{
struct student *p;
p=creat();
}