在 C++ 中将数组传递给函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14309136/
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
Passing Arrays to Function in C++
提问by Jay K
#include <iostream>
using namespace std;
void printarray (int arg[], int length) {
for (int n = 0; n < length; n++) {
cout << arg[n] << " ";
cout << "\n";
}
}
int main ()
{
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray(firstarray, 3);
printarray(secondarray, 5);
return 0;
}
This code works, but I want to understand how is the array being passed.
这段代码有效,但我想了解数组是如何传递的。
When a call is made to the printarray
function from the main function, the name of the array is being passed. The name of the array refers to the address of the first element of the array. How does this equate to int arg[]
?
当printarray
从主函数调用该函数时,将传递数组的名称。数组名是指数组第一个元素的地址。这如何等同于int arg[]
?
采纳答案by Seth Carnegie
The syntaxes
语法
int[]
and
和
int[X] // Where X is a compile-time positive integer
are exactly the same as
完全一样
int*
when in a function parameter list (I left out the optional names).
在函数参数列表中时(我省略了可选名称)。
Additionally, an array name decays to a pointer to the first element when passed to a function (and not passed by reference) so both int firstarray[3]
and int secondarray[5]
decay to int*
s.
另外,当传递到功能(而不是通过引用传递)的阵列名称衰变的指针的第一个元素,从而既int firstarray[3]
与int secondarray[5]
衰变到int*
秒。
It also happens that both an array dereference and a pointer dereference with subscript syntax (subscript syntax is x[y]
) yield an lvalue to the same element when you use the same index.
x[y]
当您使用相同的索引时,数组解引用和带有下标语法(下标语法为)的指针解引用都会产生相同元素的左值。
These three rules combine to make the code legal and work how you expect; it just passes pointers to the function, along with the length of the arrays which you cannot know after the arrays decay to pointers.
这三个规则结合起来使代码合法并按您的预期工作;它只是传递指向函数的指针,以及在数组衰减为指针后您无法知道的数组长度。
回答by DGomez
I just wanna add this, when you access the position of the array like
我只想添加这个,当你访问数组的位置时
arg[n]
arg[n]
is the same as
是相同的
*(arg + n)
than means an offset of n starting from de arg address.
*(arg + n)
than 表示从 de arg 地址开始的 n 偏移量。
so arg[0]
will be *arg
所以arg[0]
会*arg
回答by knatten
The question has already been answered, but I thought I'd add an answer with more precise terminology and references to the C++ standard.
这个问题已经得到了回答,但我想我会添加一个更精确的术语和对 C++ 标准的引用的答案。
Two things are going on here, array parameters being adjusted to pointer parameters, and array arguments being converted to pointer arguments. These are two quite different mechanisms, the first is an adjustment to the actual type of the parameter, whereas the other is a standard conversion which introduces a temporary pointer to the first element.
这里发生了两件事,数组参数被调整为指针参数,数组参数被转换为指针参数。这是两种完全不同的机制,第一种是调整参数的实际类型,而另一种是标准转换,它引入了指向第一个元素的临时指针。
Adjustments to your function declaration:
对函数声明的调整:
After determining the type of each parameter, any parameter of type “array of T” (...) is adjusted to be “pointer to T”.
在确定每个参数的类型后,任何类型为“T 数组”(...) 的参数都被调整为“指向 T 的指针”。
So int arg[]
is adjusted to be int* arg
.
所以int arg[]
被调整为int* arg
。
Conversion of your function argument:
函数参数的转换:
An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The temporary materialization conversion is applied. The result is a pointer to the first element of the array.
“NT 数组”或“T 的未知边界数组”类型的左值或右值可以转换为“指向 T 的指针”类型的纯右值。应用临时物化转换。结果是指向数组第一个元素的指针。
So in printarray(firstarray, 3);
, the lvalue firstarray
of type "array of 3 int" is converted to a prvalue (temporary) of type "pointer to int", pointing to the first element.
因此,在 中printarray(firstarray, 3);
,firstarray
“3 个 int 数组”类型的左值被转换为“指向 int 的指针”类型的纯右值(临时),指向第一个元素。
回答by Olaf Dietsche
firstarray
and secondarray
are converted to a pointer to int, when passed to printarray()
.
firstarray
并secondarray
在传递给 时转换为指向 int 的指针printarray()
。
printarray(int arg[], ...)
is equivalent to printarray(int *arg, ...)
printarray(int arg[], ...)
相当于 printarray(int *arg, ...)
However, this is not specific to C++. C has the same rules for passing array names to a function.
但是,这不是 C++ 特有的。C 对将数组名称传递给函数具有相同的规则。
回答by Bob Warner
The simple answer is that arrays are ALWAYS passed by reference and the int arg[] simply lets the compiler know to expect an array
简单的答案是数组总是通过引用传递,而 int arg[] 只是让编译器知道期望一个数组