代码:#include<stdio.h>
#define N 3
void input(struct student stu[]);
void highavg(struct student stu[]);
void avg(struct student stu[]);
struct student
{
char no[3],name[5];
unsigned int classno;
int course1grade,course2grade,course3grade;
int avg;
}stu[N];
int main()
{
struct student *p;
p=stu;
input(p);
highavg(p);
avg(p);
return 0;
}
void input(struct student stu[])
{
int i;
for(i=0;i<N;i++)
{
scanf(“%s%s%d%d%d%d”,&stu[i].no,&stu[i].name,&stu[i].classno,&stu[i].course1grade,&stu[i].course2grade,&stu[i].course3grade);
stu[i].avg=(stu[i].course1grade+stu[i].course2grade+stu[i].course3grade)/3;
}
printf(“学号 姓名 班级 课程1 课程2 课程3 平均分\n”);
for(i=0;i<N;i++)
printf(“%4s %4s %4d %4d %4d %4d %d\n”,stu[i].no,stu[i].name,stu[i].classno,stu[i].course1grade,stu[i].course2grade,stu[i].course3grade,stu[i].avg);
#define N 3
void input(struct student stu[]);
void highavg(struct student stu[]);
void avg(struct student stu[]);
struct student
{
char no[3],name[5];
unsigned int classno;
int course1grade,course2grade,course3grade;
int avg;
}stu[N];
int main()
{
struct student *p;
p=stu;
input(p);
highavg(p);
avg(p);
return 0;
}
void input(struct student stu[])
{
int i;
for(i=0;i<N;i++)
{
scanf(“%s%s%d%d%d%d”,&stu[i].no,&stu[i].name,&stu[i].classno,&stu[i].course1grade,&stu[i].course2grade,&stu[i].course3grade);
stu[i].avg=(stu[i].course1grade+stu[i].course2grade+stu[i].course3grade)/3;
}
printf(“学号 姓名 班级 课程1 课程2 课程3 平均分\n”);
for(i=0;i<N;i++)
printf(“%4s %4s %4d %4d %4d %4d %d\n”,stu[i].no,stu[i].name,stu[i].classno,stu[i].course1grade,stu[i].course2grade,stu[i].course3grade,stu[i].avg);
}
void highavg(struct student stu[])
{
int m=0,i=0;
for(i=0;i<N;i++)
{
if(stu[i].avg>stu[m].avg)
m=i;
}
printf(“输出3门课平均成绩最高的同学信息:\n”);
printf(“%4s %4s %4d %4d %4d %4d %d\n”,stu[m].no,stu[m].name,stu[m].classno,stu[m].course1grade,stu[m].course2grade,stu[m].course3grade,stu[m].avg);
}
void avg(struct student stu[])
{
int i;
int sum1=0,sum2=0,sum3=0;
int avg1=0,avg2=0,avg3=0;
for(i=0;i<3;i++)
{
sum1+=stu[i].course1grade;
sum2+=stu[i].course2grade;
sum3+=stu[i].course3grade;
}
avg1=sum1/3;
avg2=sum2/3;
avg3=sum3/3;
printf(“输出全班同学3门课的平均成绩:\n”);
printf(“course1 course2 course3 \n”);
printf(“%7d %7d %7d\n”,avg1,avg2,avg3);
}
问题:
linux显示说 array type has incomplete element type
解决方案
40
题主这样写student是个不完整类型,不能在还没出现该结构体的完整定义之前直接定义对象
把struct student{…….};的定义放到函数声明前面才行
把struct student{…….};的定义放到函数声明前面才行
#include<stdio.h> #define N 3 struct student { char no[3],name[5]; unsigned int classno; int course1grade,course2grade,course3grade; int avg; }stu[N]; void input(struct student stu[]); void highavg(struct student stu[]); void avg(struct student stu[]); int main() { struct student *p; p=stu; input(p); highavg(p); avg(p); return 0; } void input(struct student stu[]) { int i; for(i=0;i<N;i++) { scanf("%s%s%d%d%d%d",&stu[i].no,&stu[i].name,&stu[i].classno,&stu[i].course1grade,&stu[i].course2grade,&stu[i].course3grade); stu[i].avg=(stu[i].course1grade+stu[i].course2grade+stu[i].course3grade)/3; } printf("学号 姓名 班级 课程1 课程2 课程3 平均分\n"); for(i=0;i<N;i++) printf("%4s %4s %4d %4d %4d %4d %d\n",stu[i].no,stu[i].name,stu[i].classno,stu[i].course1grade,stu[i].course2grade,stu[i].course3grade,stu[i].avg); } void highavg(struct student stu[]) { int m=0,i=0; for(i=0;i<N;i++) { if(stu[i].avg>stu[m].avg) m=i; } printf("输出3门课平均成绩最高的同学信息:\n"); printf("%4s %4s %4d %4d %4d %4d %d\n",stu[m].no,stu[m].name,stu[m].classno,stu[m].course1grade,stu[m].course2grade,stu[m].course3grade,stu[m].avg); } void avg(struct student stu[]) { int i; int sum1=0,sum2=0,sum3=0; int avg1=0,avg2=0,avg3=0; for(i=0;i<3;i++) { sum1+=stu[i].course1grade; sum2+=stu[i].course2grade; sum3+=stu[i].course3grade; } avg1=sum1/3; avg2=sum2/3; avg3=sum3/3; printf("输出全班同学3门课的平均成绩:\n"); printf("course1 course2 course3 \n"); printf("%7d %7d %7d\n",avg1,avg2,avg3); }
40
前面加上声明:struct student;
#include<stdio.h> #define N 3 struct student; void input(struct student stu[]); void highavg(struct student stu[]); void avg(struct student stu[]); struct student { char no[3],name[5]; unsigned int classno; int course1grade,course2grade,course3grade; int avg; }stu[N]; int main() { struct student *p; p=stu; input(p); highavg(p); avg(p); return 0; } void input(struct student stu[]) { int i; for(i=0;i<N;i++) { scanf("%s%s%d%d%d%d",&stu[i].no,&stu[i].name,&stu[i].classno,&stu[i].course1grade,&stu[i].course2grade,&stu[i].course3grade); stu[i].avg=(stu[i].course1grade+stu[i].course2grade+stu[i].course3grade)/3; } printf("学号 姓名 班级 课程1 课程2 课程3 平均分\n"); for(i=0;i<N;i++) printf("%4s %4s %4d %4d %4d %4d %d\n",stu[i].no,stu[i].name,stu[i].classno,stu[i].course1grade,stu[i].course2grade,stu[i].course3grade,stu[i].avg); } void highavg(struct student stu[]) { int m=0,i=0; for(i=0;i<N;i++) { if(stu[i].avg>stu[m].avg) m=i; } printf("输出3门课平均成绩最高的同学信息:\n"); printf("%4s %4s %4d %4d %4d %4d %d\n",stu[m].no,stu[m].name,stu[m].classno,stu[m].course1grade,stu[m].course2grade,stu[m].course3grade,stu[m].avg); } void avg(struct student stu[]) { int i; int sum1=0,sum2=0,sum3=0; int avg1=0,avg2=0,avg3=0; for(i=0;i<3;i++) { sum1+=stu[i].course1grade; sum2+=stu[i].course2grade; sum3+=stu[i].course3grade; } avg1=sum1/3; avg2=sum2/3; avg3=sum3/3; printf("输出全班同学3门课的平均成绩:\n"); printf("course1 course2 course3 \n"); printf("%7d %7d %7d\n",avg1,avg2,avg3); }