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

时间:2022-04-02 23:07:29

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?

这个错误是什么意思,我怎么修正它呢?

5 个解决方案

#1


32  

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 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.

将实际传递一个指针到二维数组的第一个子数组(“board_width”完全被忽略,就像当int数组[]接受指针时只有一个维度的退化情况一样),这可能不是您想要的(因为您显式地要求引用)。因此,做参考,在参数使用sizeof运算符数组将产生sizeof(int[board_width][board_height])(如果你想做论点本身),而在第二种方法(将参数声明为数组,因此编译器转换为一个指针)将产生sizeof(int(*)[board_height]),因此只是一个指针的运算符。

#2


21  

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.

只有在调用函数需要对第一个数组索引的大小进行静态检查时才会出现这种情况。

#3


6  

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

#4


1  

Syntax is not correct.

语法是不正确的。

Lets take an example of 1D Array

让我们举一个一维数组的例子

 int a[] = {1,2,3};
 int (&p) [3] = a;

so you can do like below for 2D array

你可以像下面这样处理2D数组

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

#5


0  

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.

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

#1


32  

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 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.

将实际传递一个指针到二维数组的第一个子数组(“board_width”完全被忽略,就像当int数组[]接受指针时只有一个维度的退化情况一样),这可能不是您想要的(因为您显式地要求引用)。因此,做参考,在参数使用sizeof运算符数组将产生sizeof(int[board_width][board_height])(如果你想做论点本身),而在第二种方法(将参数声明为数组,因此编译器转换为一个指针)将产生sizeof(int(*)[board_height]),因此只是一个指针的运算符。

#2


21  

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.

只有在调用函数需要对第一个数组索引的大小进行静态检查时才会出现这种情况。

#3


6  

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

#4


1  

Syntax is not correct.

语法是不正确的。

Lets take an example of 1D Array

让我们举一个一维数组的例子

 int a[] = {1,2,3};
 int (&p) [3] = a;

so you can do like below for 2D array

你可以像下面这样处理2D数组

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

#5


0  

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.

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