C++ 通过引用传递数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5724171/
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-28 18:43:44  来源:igfitidea点击:

Passing an array by reference

c++arrays

提问by John DB

How does passing a statically allocated array by reference work?

通过引用传递静态分配的数组如何工作?

void foo(int (&myArray)[100])
{
}

int main()
{
    int a[100];
    foo(a);
}

Does (&myArray)[100]have any meaning or its just a syntax to pass any array by reference? I don't understand separate parenthesis followed by big brackets here. Thanks.

是否(&myArray)[100]有任何意义或它只是一个语法按引用传递任何阵列?我不明白单独的括号后面是大括号。谢谢。

回答by Erik

It's a syntax for array references - you need to use (&array)to clarify to the compiler that you want a reference to an array, rather than the (invalid) array of references int & array[100];.

这是数组引用的语法 - 您需要使用(&array)来向编译器说明您想要对数组的引用,而不是(无效的)引用数组int & array[100];

EDIT: Some clarification.

编辑:一些澄清。

void foo(int * x);
void foo(int x[100]);
void foo(int x[]);

These three are different ways of declaring the same function. They're all treated as taking an int *parameter, you can pass any size array to them.

这三种是声明相同函数的不同方式。它们都被视为接受一个int *参数,您可以将任何大小的数组传递给它们。

void foo(int (&x)[100]);

This only accepts arrays of 100 integers. You can safely use sizeofon x

这仅接受 100 个整数的数组。您可以放心地使用sizeofx

void foo(int & x[100]); // error

This is parsed as an "array of references" - which isn't legal.

这被解析为“引用数组”——这是不合法的。

回答by Martin York

It's just the required syntax:

这只是所需的语法:

void Func(int (&myArray)[100])

^ Pass array of 100 intby reference the parameters name is myArray;

^int通过引用传递 100 个数组,参数名称为myArray;

void Func(int* myArray)

^ Pass an array. Array decays to a pointer. Thus you lose size information.

^ 传递一个数组。数组衰减为指针。因此,您会丢失尺寸信息。

void Func(int (*myFunc)(double))

^ Pass a function pointer. The function returns an intand takes a double. The parameter name is myFunc.

^ 传递函数指针。该函数返回一个int并接受一个double。参数名称是myFunc.

回答by cpx

It is a syntax. In the function arguments int (&myArray)[100]parenthesis that enclose the &myArrayare necessary. if you don't use them, you will be passing an array of referencesand that is because the subscript operator []has higher precedence over the & operator.

它是一种语法。在函数参数int (&myArray)[100]中,括号&myArray是必要的。如果你不使用它们,你会传递一个array of references,就是因为subscript operator []拥有更高的优先级& operator

E.g. int &myArray[100] // array of references

例如 int &myArray[100] // array of references

So, by using type construction ()you tell the compiler that you want a reference to an array of 100 integers.

因此,通过使用,type construction ()您可以告诉编译器您想要一个包含 100 个整数的数组的引用。

E.g int (&myArray)[100] // reference of an array of 100 ints

例如 int (&myArray)[100] // reference of an array of 100 ints

回答by Eduardo A. Fernández Díaz

Arrays are default passed by pointers. You can try modifying an array inside a function call for better understanding.

数组默认通过指针传递。您可以尝试在函数调用中修改数组以更好地理解。