在用二维数组名作为参数传递时容易出现Segmention Error。这是因为不能正确为二维数组中元素寻址的问题,正确的方法如下:
1. 用指向一维数组的指针变量,如下例子所示:
#include <stdlib.h>
#include <stdio.h>
#define N 4
void testArr(int (*a)[N], int m)
{
for(int i = 0; i < m; ++i)
for(int j = 0; j < N; ++j)
{
printf("a[%d][%d] = %d\n", i, j, a[i][j]);
}
}
int main()
{
int a[2][N] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
testArr(a, 2);
}......
阅读全文