我的函数原型(prototype)是
int** rotate(int **arr, int row, int col, int fl);
其中arr
是二维数组,row
和col
分别是二维数组的行数和列数,fl
是一个标志变量。如果fl
的值为0
则数组将向右旋转,如果fl为1
则数组将向左旋转。
我按如下方式调用该函数:
int **res= rotate(arr, row, col, fl);
但是我收到了一个警告和一个注释
[Warning] passing argument 1 of 'rotate' from incompatible pointer type.
[Note] expected 'int **' but argument is of type 'int (*)[20]'
最佳答案
指向指针的指针与指向数组的指针不同。数组到指针的衰减只能发生在最左侧(例如 int [3][20]
到 int (*)[20]
)。
将函数声明更改为
int** rotate(int (*arr)[20], int row, int col, int fl);
或更明显的是,
int** rotate(int arr[][20], int row, int col, int fl);
请注意,您必须在编译时修复大小。
关于c - 如何将二维数组传递给C中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47904671/