您的位置:首页 > 其它

多维数组的指针做函数参数

2007-05-21 09:15 316 查看
用指针变量作形参以接受实参数组名传递来的地址时,有两种方法:
(1)用指向变量的指针变量;
(2) 用指向一维数组的指针变量.
例:有一个班,3个学生,各4门课,计算总平均分数,以及第n个学生的成绩.
int main()
{
void average(float *p,int n);
void search(float(*p)[4],int n);
flag score[3][4]={{65,67,70,60},{80,87,90,81},{99,90,100,98}};
average(*score,12);
search(score,2);
}
void average(float *p,int n)
{
float *p_end;
float sum=0,aver;
p_end =p+n-1;
for(;p<=p_end;p++)
sum+=*p;
aver = sum/n;
printf("average = %5.2f/n",aver);
}

void search(float (*p)[4],int n)
{
int i;
printf("the score of NO.%d are/n",n);
for(i=0;i<4;i++)
printf("%5.2f",*(*(p+n)+i));
}
运行结果:
average = 82.25
the score of No.2 are:
99.00,90.00,100.00,98.00
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐