关于C语言中结构体传递指针的问题

来源:百度知道 编辑:UC知道 时间:2024/07/04 18:25:56
有一个班有4个学生,5门课程。编写一个函数实现“求第一门课程的平均分”。
#include<stdio.h>
void average(struct student *);
struct student
{
double English;
double Math;
double Chinese;
double Physics;
}stu[4]={{98.5,95,85.5,76},{78,98.5,90,96},{59,80.5,100,70},{92,78.5,89,91.5}};
int main()
{
average(stu);
return 0;
}
void average(struct student *p)
{
double aver=0;
for(int i=0;i<4;i++,p++)
{
aver=aver+p->English;
}
printf("The average of the first course (Endilsh) is : %d\n",aver);
}

请问为什么我的输出结果是“0”?
是哪里不对么?我感觉数组根本没有传递成功。。。

不是结果不对,是你输出格式错了,double类型输出用%lf
你可以输出到小数点一位
printf("The average of the first course (Endilsh) is : %.1lf\n",aver);

printf("The average of the first course (Endilsh) is : %f\n",aver/4);