[总结]C语言二维数组作为函数的参数
前言: 今天在实现装配线调度程序时候,用到了二维数组,并将其作为函数的参数。在写程序的时候,遇到一些问题,即二维数组做函数的参数应该如何正确表示。我写程序的错误如下程序所示: 1 #include <cstdio> 2 void print(int *a[3]) 3 { 4 printf("%d\n",a[0][0]); 5 } 6 7 int main() 8 { 9 int a[2][3] = {1,2,3,4,5,6}; 10 print(a); 11 return 0; 12 } 编译程序时候,在第10行提示错误信息: |10|error: cannot convert 'int (*)[3]' to 'int**' for argument '1' to 'void print(int**)'|。 根据错误提示我明白了, int *a[3]表示一个一维数组,数组的数据类型为整型指针(int*),数组的大小为3,这是因为[]的优先级高于*的优先级。如是我将程序改写如下,顺利通过编译,得到正确结果。 1 #include <cstdio> 2 void print(int (*a)[3]) //用括号将指针括起来 3 { 4 printf("%d\n",a[0][0]); 5 } 6 7 int main() 8 { 9 int a[2][3] = {1,2,3,4,5