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?
- 是来自C的遗产吗?
- What does the standard say?
- 标准是怎么说的?
- Why is the strict type-safety of C++ dropped?
- 为什么c++的严格类型安全降低了?
3 个解决方案
#1
70
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指定符-seq和声明符决定。在确定每个参数的类型,任何类型的参数数组”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
一个是一个
Move right - we find a )
so we reverse direction looking for the (
. As we move left we pass &
向右移动-我们找到a)所以我们反向寻找(。当我们向左移动时,我们通过&
'a' is a reference
“一”是一个参考
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个字符的数组的引用
#2
13
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';
}
#3
1
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
#1
70
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指定符-seq和声明符决定。在确定每个参数的类型,任何类型的参数数组”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
一个是一个
Move right - we find a )
so we reverse direction looking for the (
. As we move left we pass &
向右移动-我们找到a)所以我们反向寻找(。当我们向左移动时,我们通过&
'a' is a reference
“一”是一个参考
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个字符的数组的引用
#2
13
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';
}
#3
1
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