C++ 当函数具有特定大小的数组参数时,为什么要用指针替换它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1328223/
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
When a function has a specific-size array parameter, why is it replaced with a pointer?
提问by CsTamas
Given the following program,
鉴于以下程序,
#include <iostream>
using namespace std;
void foo( char a[100] )
{
cout << "foo() " << sizeof( a ) << endl;
}
int main()
{
char bar[100] = { 0 };
cout << "main() " << sizeof( bar ) << endl;
foo( bar );
return 0;
}
outputs
产出
main() 100
foo() 4
- Why is the array passed as a pointer to the first element?
- Is it a heritage from C?
- What does the standard say?
- Why is the strict type-safety of C++ dropped?
- 为什么将数组作为指向第一个元素的指针传递?
- 它是C的遗产吗?
- 标准怎么说?
- 为什么取消了 C++ 的严格类型安全?
回答by Richard Corden
Yes it's inherited from C. The function:
是的,它继承自 C。函数:
void foo ( char a[100] );
Will have the parameter adjusted to be a pointer, and so becomes:
将参数调整为指针,因此变为:
void foo ( char * a );
If you want that the array type is preserved, you should pass in a reference to the array:
如果您希望保留数组类型,则应传入对数组的引用:
void foo ( char (&a)[100] );
C++ '03 8.3.5/3:
C++ '03 8.3.5/3:
...The type of a function is determined using the following rules. The type of each parameter is determined from its own decl-specifier-seq and declarator. After determining the type of each parameter, any parameter of type "array of T" or "function returning T" is adjusted to be "pointer to T" or "pointer to function returning T," respectively....
...函数的类型使用以下规则确定。每个参数的类型由它自己的 decl-specifier-seq 和 declarator 确定。确定每个参数的类型后,任何类型为“T的数组”或“返回T的函数”类型的参数分别调整为“指向T的指针”或“指向返回T的函数的指针”……
To explain the syntax:
解释语法:
Check for "right-left" rule in google; I found one description of it here.
在谷歌中检查“右-左”规则;我在这里找到了一个描述。
It would be applied to this example approximately as follows:
它将大致如下应用于此示例:
void foo (char (&a)[100]);
Start at identifier 'a'
从标识符“a”开始
'a' is a
'a' 是一个
Move right - we find a )
so we reverse direction looking for the (
. As we move left we pass &
向右移动 - 我们找到了 ,)
所以我们反向寻找(
。当我们向左移动时,我们经过&
'a' is a reference
'a' 是一个参考
After the &
we reach the opening (
so we reverse again and look right. We now see [100]
在&
我们到达开口之后,我们(
再次倒车并向右看。我们现在看到[100]
'a' is a reference to an array of 100
'a' 是对 100 数组的引用
And we reverse direction again until we reach char
:
我们再次反转方向,直到我们到达char
:
'a' is a reference to an array of 100 chars
'a' 是对 100 个字符的数组的引用
回答by sbi
Yes. In C and C++ you cannot pass arrays to functions. That's just the way it is.
是的。在 C 和 C++ 中,您不能将数组传递给函数。就是那样子。
Why are you doing plain arrays anyway? Have you looked at boost
/std::tr1::array
/std::array
or std::vector
?
你为什么要做普通数组?你看过boost
/ std::tr1::array
/std::array
或std::vector
?
Note that you can, however, pass a reference to an array of arbitrary length to a function template. Off the top of my head:
但是请注意,您可以将任意长度数组的引用传递给函数模板。在我的头顶:
template< std::size_t N >
void f(char (&arr)[N])
{
std::cout << sizeof(arr) << '\n';
}
回答by nickolay
There is magnificent word in C/C++ terminology that is used for static arrays and function pointers - decay. Consider the following code:
C/C++ 术语中有一个用于静态数组和函数指针的华丽词——衰变。考虑以下代码:
int intArray[] = {1, 3, 5, 7, 11}; // static array of 5 ints
//...
void f(int a[]) {
// ...
}
// ...
f(intArray); // only pointer to the first array element is passed
int length = sizeof intArray/sizeof(int); // calculate intArray elements quantity (equals 5)
int ptrToIntSize = sizeof(*intArray); // calculate int * size on your system