用多维数组名作函数参数
#include <stdio.h>
int max_value(int array[][4]);
int main( )
{
int a[3][4]= {{11,32,45,67},{22,44,66,88},{15,72,43,37}};
printf("max value is %d\n", max_value(a));
return 0;
}
int max_value(int array[][4])
{
int i,j,max;
max=array[0][0];
for( i=0; i<3; i++)
for(j=0; j<4; j++)
if(array[i][j]>max)
max=array[i][j];
return max;
}
工程中更普遍的做法:行数作为形参
#include <stdio.h>
int max_value(int array[][4], int);
int main( )
{
int a[3][4]= {{11,32,45,67},{22,44,66,88},{15,72,43,37}};
printf("max value is %d\n", max_value(a, 3));
return 0;
}
int max_value(int array[][4], int n)
{
int i,j,max;
max=array[0][0];
for( i=0; i<n; i++)
for(j=0; j<4; j++)
if(array[i][j]>max)
max=array[i][j];
return max;
}
例 3个学生4门课,计算总平均分,并输出第n个学生成绩
#include <stdio.h>
void average(int *p, int n);
void search(int (*p)[4], int n);
int main()
{
int score[3][4]=
{
{65,67,79,60},{80,87,90,81},
{90,99,100,98}
};
average(*score,12);
search(score,2);
return 0;
}
void average(int *p, int n)
{
int *p_end, sum=0,aver;
p_end=p+n-1;
for(; p<=p_end; p++)
sum=sum+(*p);
aver=sum/n;
printf("average=%4d\n",aver);
}
void search(int (*p)[4], int n)
{
int i;
printf("No.%d :",n);
for(i=0; i<4; i++)
printf("%4d",*(*(p+n)+i));
printf("\n");
}