【发布时间】:2013-01-18 01:59:45
【问题描述】:
可能重复:
Passing multidimensional arrays as function arguments in C
在 C 中, 如果我想让一个函数接收一个二维数组,我可以在函数参数中使用 * 符号吗
int (int my2dary[][10]); //This is what I do not want.
【问题讨论】:
可能重复:
Passing multidimensional arrays as function arguments in C
在 C 中, 如果我想让一个函数接收一个二维数组,我可以在函数参数中使用 * 符号吗
int (int my2dary[][10]); //This is what I do not want.
【问题讨论】:
如果您的问题是在编译时不知道数组的大小,您可能需要:
int func(int *array, int size)
{
int n,m;
...
array[m*size+n]; /* = array[m][n] if in the caller: int array[x][size]; */
}
您可以选择(并且很可能您需要)传递第二个大小参数 (x) 来测试数组边界
【讨论】:
是的,你传递了一个指向 int 数组的指针
int func(int (*my2dary)[10]);
你叫它
int a[5][10];
func(a);
虽然func不知道my2dary中有多少元素,所以你也必须给出一个数字
int func(int n, int (*my2dary)[10]);
并调用
int a[5][10];
func(5, a);
见How to interpret complex C/C++ declarations 或The ``Clockwise/Spiral Rule''。
【讨论】: