0

Possible Duplicate:
Passing multidimensional arrays as function arguments in C

In C, if I want a function to receive a 2-D array, can I use * notation for the function parameter

int (int my2dary[][10]);  //This is what I do not want.
4

2 回答 2

0

Yes, you pass a pointer to an array of int

int func(int (*my2dary)[10]);

and you call it

int a[5][10];
func(a);

Although, func doesn't know how many elements are in my2dary, so you must give a number too

int func(int n, int (*my2dary)[10]);

and call

int a[5][10];
func(5, a);

See How to interpret complex C/C++ declarations or The ``Clockwise/Spiral Rule''.

于 2013-02-03T01:41:47.177 回答
0

如果您的问题是您在编译时不知道数组的大小,您可能需要:

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) 以便能够测试数组边界

于 2013-02-03T02:33:02.347 回答