[英]How do I pass a reference to a two-dimensional array to a function?
I am trying to pass a reference to a two-dimensional array to a function in C++. 我试图将二维数组的引用传递给C ++中的函数。 I know the size of both dimensions at compile time.
我在编译时知道两个维度的大小。 Here is what I have right now:
这就是我现在所拥有的:
const int board_width = 80;
const int board_height = 80;
void do_something(int[board_width][board_height]& array); //function prototype
But this doesn't work. 但这不起作用。 I get this error from g++:
我从g ++中得到这个错误:
error: expected ‘,’ or ‘...’ before ‘*’ token
What does this error mean, and how can I fix it? 这个错误意味着什么,我该如何解决?
If you know the size at compile time, this will do it: 如果您在编译时知道大小,那么这样做:
//function prototype
void do_something(int (&array)[board_width][board_height]);
Doing it with 做到这一点
void do_something(int array[board_width][board_height]);
Will actually pass a pointer to the first sub-array of the two dimensional array ("board_width" is completely ignored, as with the degenerate case of having only one dimension when you have int array[]
accepting a pointer), which is probably not what you want (because you explicitly asked for a reference). 实际上会传递指向二维数组的第一个子数组的指针(“board_width”被完全忽略,就像当你有
int array[]
接受指针时只有一个维度的退化情况一样),这可能不是你想要什么(因为你明确要求参考)。 Thus, doing it with the reference, using sizeof on the parameter sizeof array
will yield sizeof(int[board_width][board_height])
(as if you would do it on the argument itself) while doing it with the second method (declaring the parameter as array, thus making the compiler transform it to a pointer) will yield sizeof(int(*)[board_height])
, thus merely the sizeof of a pointer. 因此,使用参数
sizeof array
在参数sizeof array
上使用sizeof将产生sizeof(int[board_width][board_height])
(就像你在参数本身上做的那样),同时使用第二种方法(声明参数)作为数组,从而使编译器将其转换为指针)将产生sizeof(int(*)[board_height])
,因此只是指针的sizeof。
Although you can pass a reference to an array, because arrays decay to pointers in function calls when they are not bound to a reference parameters and you can use pointers just like arrays, it is more common to use arrays in function calls like this: 虽然你可以传递对数组的引用,因为当数组未绑定到引用参数时,数组会在函数调用中衰减为指针,并且你可以像数组一样使用指针,在函数调用中使用数组更常见,如下所示:
void ModifyArray( int arr[][80] );
or equivalently 或者等价的
void ModifyArray( int (*arr)[80] );
Inside the function, arr can be used in much the same way as if the function declaration were: 在函数内部,arr的使用方式与函数声明的方式非常相似:
void ModifyArray( int (&arr)[80][80] );
The only case where this doesn't hold is when the called function needs a statically checked guarantee of the size of the first array index. 唯一不支持的情况是被调用函数需要静态检查第一个数组索引大小的保证。
You might want to try cdecl or c++decl . 您可能想尝试cdecl或c ++ decl 。
% c++decl
c++decl> declare i as reference to array 8 of array 12 of int
int (&i)[8][12]
c++decl> explain int (&i)[8][12]
declare i as reference to array 8 of array 12 of int
c++decl> exit
Syntax is not correct. 语法不正确。
Lets take an example of 1D Array 让我们举一个数组的例子
int a[] = {1,2,3};
int (&p) [3] = a; // p is pointing array a
So you can do same for 2D array as shown below 因此,您可以对2D阵列执行相同操作,如下所示
const int board_width = 80;
const int board_height = 80;
void do_something(int (&array) [board_width][board_height]);
I think this is what you want: 我想这就是你想要的:
void do_something(int array[board_width][board_height]);
You can't pass an array of references to a function. 您不能传递对函数的引用数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.