在ANSI C标准中允许用结构变量作函数参数进行整体传送。但是这种传送要将全部成员逐个传送,特别是成员为数组时将会使传送的时间和空间开销很大,严重地降低了程序的效率。因此最好的办法就是使用指针,即用指针变量作函数参数进行传送。这时由实参传向形参的只是地址,从而减少了时间和空间的开销。
【例11-7】计算一组学生的平均成绩和不及格人数。用结构指针变量作函数参数编程。
struct stu{
int num;
char *name;
char sex;
float score;
}boy[5]={
{101,"Li ping","M",45},
{102,"Zhang ping","M",62.5},
{103,"He fang","F",92.5},
{104,"Cheng ling","F",87},
{105,"Wang ming","M",58}
};
main(){
struct stu *ps;
void ave(struct stu *ps);
ps=boy;
ave(ps);
}
void ave(struct stu *ps){
int c=0,i;
float ave,s=0;
for(i=0;i5;i++,ps++){
s+=ps->score;
if(ps->score60) c+=1;
}
printf("s=%f\n",s);
ave=s/5;
printf("average=%f\ncount=%d\n",ave,c);
}
struct stu{ int num; char *name; char sex; float score;}boy[5]={ {101,"Li ping","M",45}, {102,"Zhang ping","M",62.5}, {103,"He fang","F",92.5}, {104,"Cheng ling","F",87}, {105,"Wang ming","M",58}};main(){ struct stu *ps; void ave(struct stu *ps); ps=boy; ave(ps);}void ave(struct stu *ps){ int c=0,i; float ave,s=0; for(i=0;iscore; if(ps->score
如果觉得《C语言结构体指针变量作函数参数》对你有帮助,请点赞、收藏,并留下你的观点哦!