C++ 如何将二维数组的引用传递给函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/404232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:10:09  来源:igfitidea点击:

How do I pass a reference to a two-dimensional array to a function?

c++arrays

提问by Johannes Schaub - litb

I am trying to pass a reference to a two-dimensional array to a function in C++. I know the size of both dimensions at compile time. Here is what I have right now:

我正在尝试将二维数组的引用传递给 C++ 中的函数。我知道编译时两个维度的大小。这是我现在所拥有的:

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?

这个错误是什么意思,我该如何解决?

回答by Johannes Schaub - litb

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). Thus, doing it with the reference, using sizeof on the parameter sizeof arraywill 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.

实际上会传递一个指向二维数组的第一个子数组的指针(“board_width”被完全忽略,就像int array[]接受指针时只有一个维度的退化情况一样),这可能不是你想要的(因为您明确要求提供参考)。因此,使用引用执行此操作,在参数上使用 sizeofsizeof array将产生sizeof(int[board_width][board_height])(就像您对参数本身执行此操作一样),而使用第二个方法执行此操作(将参数声明为数组,从而使编译器将其转换为指针) 将产生sizeof(int(*)[board_height]),因此只是指针的大小。

回答by CB Bailey

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.

这不成立的唯一情况是当被调用的函数需要第一个数组索引的大小的静态检查保证时。

回答by Mr.Ree

You might want to try cdeclor c++decl.

您可能想尝试cdeclc++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

回答by Giriraj Pawar

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

所以你可以对二维数组做同样的事情,如下所示

const int board_width = 80;
const int board_height = 80;
void do_something(int (&array) [board_width][board_height]); 

回答by Jeremy Ruten

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.

您不能将引用数组传递给函数。