一定要注意的是,二维数组不完全等价与二维指针,二维数组经常与数组指针联系起来 。
# include <stdio.h>
void *getscore(int pos,int (*pstu)[4],int**pstudent)
{
*pstudent=(int*)(pstu+pos);
}
int main()
{
int score[3][4]={
{12,34,66,78},{33,66,88,99},{66,77,99,100}};
int *pstudent;
int pos,i;
for(pos=0;pos<3;pos++)
{
pstudent=getscore(pos,score,&pstudent);
for(i=0;i<4;i++)
{ if(*pstudent<60)
{ printf("%d %d ",pos,*pstudent);}
++pstudent;
}
}
return 0;
}
注意看我红色的部分,在函数定义的时候中间的第二个形参是一个数组指针,但是我们在调用的时候,函数的实参却是一个二维数组名,是不是很神奇。
事实上,正如我在图片里面写的那样,无论是二维数组a还是一维子数组(a+1),它们都是可以代表各自对应的首元素地址。定义函数的形参是数组指针与调用函数时是二维数组名并不冲突。
在函数的定义里中int (*pstu)[4]这个形参其实刚好对应着(a+0)的地址,而(a+0)这个一维数组中也刚好有四个元素,这不是巧合,是一一对应。