C语言中结构体成员变量的赋值困惑

2025-03-22 22:22:56
推荐回答(1个)
回答1:

#include 
#define N 8

typedef struct
{
char num[10];
double s[N];
double ave;
}STREC;
void fun(STREC *a)  //你原来的是实参 改变只在函数内部起效 
                    //直说就是这里的操作不会改变main函数中结构体对象中ave的值
                    //我这里用结构体指针
{
int i;
a->ave=0.0;
for (i=0;i a->ave=a->ave+a->s[i];
a->ave=a->ave/N;
}
void main()
{
STREC s={"GA005",85.5,76,69.5,85,91,72,64.5,87.5};
int i;
fun(&s);   //传地址
printf("\nThe %s's student data:\n",s.num);
for (i=0;i printf("%4.1f\n",s.s[i]);
printf("\nave=%7.2f\n",s.ave); 
}

The GA005's student data:
85.5
76.0
69.5
85.0
91.0
72.0
64.5
87.5

ave=  78.88
Press any key to continue