c语言问题,和结构体有关

就是一个 信息系统,func1的功能是信息录入,学号num为零的表示结束录入,主函数就先不谢了,后面有错误信息,前面用结构体录入了学号,名字,性别,年龄,科目成绩
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)
#define NULL 0
struct score /*用来记录各科成绩准备后面补充的*/
{
int math;

};
struct student /*声明一个结构体用来记录学生信息*/
{
int num;
char name[20];
char sex;
int age;
struct score subject;
struct student *next;
};

void funct1()
{
printf("请录入学生信息,学号为0代表录入完成\n");
struct student *p1,*p2;
struct student.subject math;
p1=p2=( struct student*) malloc(LEN); /* 从这里开始时想做第一个信息录入*/
printf("请输入学号\n");
scanf("%d",&p1->num);
printf("\n请输入该生姓名");
gets(p1->name);
printf("\n请输入该生性别\n");
for(;p1->sex=='f'||p1->sex=='m';)
{
p1->sex=getchar();
printf( "\nerror,please input again:\n");
}
printf("\n请输入该生年龄\n");
scanf("%d",&p1->age);
printf("\n请输入该生成绩,以逗号隔开\n");
scanf("%f",&p1->subject->math); /*到这里让p1指向第一组数据结束*/
head=NULL;
while (p1->num!=0) /*循环跳出的条件是学号num为零*/
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*) malloc(LEN);
printf("请输入学号\n");
scanf("%d",&p1->num);
printf("\n请输入该生姓名");
gets(p1->name);
printf("\n请输入该生性别\n");
for(;p1->sex=='f'||p1->sex=='m';)
{
p1->sex=getchar();
printf( "\nerror,please input again:\n");
}
printf("\n请输入该生年龄\n");
scanf("%d",&p1->age);
printf("\n请输入该生成绩,以逗号隔开\n");
scanf("%f",&p1->subject->math);
}
p2->next=NULL;
}

以下是错误信息,好像一直都是一个地方出错
:\新建文件夹\student information.c(6) : warning C4005: 'NULL' : macro redefinition
d:\microsoft visual studio\vc98\include\stdio.h(212) : see previous definition of 'NULL'
D:\新建文件夹\student information.c(26) : error C2143: syntax error : missing ';' before 'type'
D:\新建文件夹\student information.c(27) : error C2143: syntax error : missing ';' before 'type'
D:\新建文件夹\student information.c(28) : error C2065: 'p1' : undeclared identifier
D:\新建文件夹\student information.c(28) : error C2065: 'p2' : undeclared identifier
D:\新建文件夹\student information.c(28) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct student *'
D:\新建文件夹\student information.c(30) : error C2223: left of '->num' must point to struct/union
D:\新建文件夹\student information.c(32) : error C2223: left of '->name' must point to struct/union
D:\新建文件夹\student information.c(32) : error C2198: 'gets' : too few actual parameters

第1个回答  2011-05-21
#include <stdio.h>
#include <malloc.h>
struct score
{
int math;
};

struct student
{
int num;
char name[20];
char sex;
int age;
struct score *subject;
struct student *next;
};
struct student* max();
struct student* max()
{
struct student *head,*p;
head=p=NULL;
int x;
do
{
if(head==NULL)
head=p=(struct student *)malloc(sizeof(student));
else
{
p->next=(struct student *)malloc(sizeof(student));
p=p->next;
}
printf("输入编号:");
scanf("%d",&p->num);
getchar();
printf("输入姓名:");
gets(p->name);
printf("输入性别:");
scanf("%c",&p->sex);
printf("输入年龄:");
scanf("%d",&p->age);
printf("输入成绩:");
getchar();
p->subject=(struct score*)malloc(sizeof(score));
scanf("%d",&p->subject->math);
printf("输入0退出录入否则继续录入");
scanf("%d",&x);
} while(x!=0);
p->next=NULL;
return head;
}
void show(struct student *head);
void show(struct student *head)
{
while (head!=NULL)
{
printf("编号:%d ",head->num);
printf("姓名:%s ",head->name);
printf("性别:%c ",head->sex);
printf("年龄:%d ",head->age);
printf("成绩:%d \n",head->subject->math);
head=head->next;
}
}
void main()
{
struct student *head;
head=max();
show(head);
}
第2个回答  2011-05-21
NULL在系统中有定义无需自己定义。
struct student *p1,*p2,*head;//你忘记定义后面的head要定义
struct student.subject math;//这句好像没有用。
scanf("%f",&p1->subject.math); //subject.math 改为.指针才用-> /*到这里让p1指向第一组数据结束*/
int n=n+1; //你忘记定义n了
scanf("%f",&p1->subject.math);//subject.math 改为.指针才用->
你试试看还有什么问题本回答被提问者采纳
第3个回答  2011-05-24
#include <stdio.h>
#include <malloc.h>
struct score
{
int math;
};

struct student
{
int num;
char name[20];
char sex;
int age;
struct score *subject;
struct student *next;
};
struct student* max();
struct student* max()
{
struct student *head,*p;
head=p=NULL;
int x;
do
{
if(head==NULL)
head=p=(struct student *)malloc(sizeof(student));
else
{
p->next=(struct student *)malloc(sizeof(student));
p=p->next;
}
printf("输入编号:");
scanf("%d",&p->num);
getchar();
printf("输入姓名:");
gets(p->name);
printf("输入性别:");
scanf("%c",&p->sex);
printf("输入年龄:");
scanf("%d",&p->age);
printf("输入成绩:");
getchar();
p->subject=(struct score*)malloc(sizeof(score));
scanf("%d",&p->subject->math);
printf("输入0退出录入否则继续录入");
scanf("%d",&x);
} while(x!=0);
p->next=NULL;
return head;
}
void show(struct student *head);
void show(struct student *head)
{
while (head!=NULL)
{
printf("%d ",head->num);
printf("%s ",head->name);
printf("%c ",head->sex);
printf("%d ",head->age);
printf("%d \n",head->subject->math);
head=head->next;
}
}
void main()
{
struct student *head;
head=max();
show(head);
}
相似回答