#include<stdio.h>
struct student
{
int num;
char classname[20];
char name[20];
float score[3];
float aver;
};
student average(student stu[]);
int hightest(student stu[]);
int main()
{
student stu[2];
student *p = stu;
int i;
for(i = 0; i < 2; i++) //输入数据
{
scanf("%d %s %s %lf %lf %lf",&stu[i].num,&stu[i].classname,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}
//打印数据
average(p); // 调用函数,计算aver的值
for(i=0;i<2;i++)
{
printf("%s 的平均成绩为:%lf\n",stu[i].name,stu[i].aver);
}
return 0;
}
student *average(student *stu[]) //计算平均分
{
int i;
for(i=0;i<2;i++)
{
stu[i]->aver = (stu[i]->score[0]+stu[i]->score[1]+stu[i]->score[2])/3.0;
}
return stu;
}
int hightest(student stu[]) //最高分的人
{
int k=0,i;
float s = stu[0].aver;
for (i=0;i<5;i++)
{
if (stu[i].aver>s)
k = i;
}
return k;
}
返回一个指针时出错,第40行
40 行提示错误:
error C2440: 'return' : cannot convert from 'struct student *[]' to 'struct student *'
显然,你返回的类型不正确。
你的函数 average() 目的是计算每个学生的平均分,根据函数目的,根本不需要返回任何值。