C++ [方括号]和*星号的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1790704/
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
Difference between [square brackets] and *asterisk
提问by bobobobo
If you write a C++ function like
如果你写一个 C++ 函数,比如
void readEmStar( int *arrayOfInt ) { }
vs a C++ function like:
vs 一个 C++ 函数,如:
void readEmSquare( int arrayOfInt[] ) { }
What is the difference between using [square brackets] vs *asterisk, and does anyone have a style guide as to which is preferrable, assuming they are equivalent to the compiler?
使用 [方括号] 与 *星号有什么区别,假设它们等同于编译器,是否有人有关于哪种更可取的样式指南?
For completeness, an example
为了完整起见,举个例子
void readEmStar( int *arrayOfInt, int len )
{
for( int i = 0 ; i < len; i++ )
printf( "%d ", arrayOfInt[i] ) ;
puts("");
}
void readEmSquare( int arrayOfInt[], int len )
{
for( int i = 0 ; i < len; i++ )
printf( "%d ", arrayOfInt[i] ) ;
puts("");
}
int main()
{
int r[] = { 2, 5, 8, 0, 22, 5 } ;
readEmStar( r, 6 ) ;
readEmSquare( r, 6 ) ;
}
回答by Brian R. Bondy
When you use the type char x[]
instead of char *x
without initialization, you can consider them the same. You cannot declare a new type as char x[]
without initialization, but you can accept them as parameters to functions. In which case they are the same as pointers.
当您使用类型char x[]
而不是char *x
没有初始化时,您可以将它们视为相同。你不能在char x[]
没有初始化的情况下声明一个新类型,但你可以接受它们作为函数的参数。在这种情况下,它们与指针相同。
When you use the type char x[]
instead of char *x
with initialization, they are completely 100% different.
当您使用类型char x[]
而不是char *x
使用初始化时,它们完全 100% 不同。
Example of how char x[]
is different from char *x
:
如何char x[]
与以下不同的示例char *x
:
char sz[] = "hello";
char *p = "hello";
sz
is actually an array, not a pointer.
sz
实际上是一个数组,而不是一个指针。
assert(sizeof(sz) == 6);
assert(sizeof(sz) != sizeof(char*));
assert(sizeof(p) == sizeof(char*));
Example of how char x[]
is the same as char *x
:
如何char x[]
与以下相同的示例char *x
:
void test1(char *p)
{
assert(sizeof(p) == sizeof(char*));
}
void test2(char p[])
{
assert(sizeof(p) == sizeof(char*));
}
Coding style for passing to functions:
传递给函数的编码风格:
It really doesn't matter which one you do. Some people prefer char x[]
because it is clear that you want an array passed in, and not the address of a single element.
你做哪一个真的无所谓。有些人更喜欢,char x[]
因为很明显你想要传入一个数组,而不是单个元素的地址。
Usually this is already clear though because you would have another parameter for the length of the array.
通常这已经很清楚了,因为您将有另一个参数来表示数组的长度。
Further reading:
进一步阅读:
Please see this post entitled Arrays are not the same as pointers!
回答by Alexey Malistov
C++ Standard 13.1.3
C++ 标准 13.1.3
— Parameter declarations that differ only in a pointer * versus an array [] are equivalent. That is, the array declaration is adjusted to become a pointer declaration (8.3.5). Only the second and subsequent array dimensions are significant in parameter types (8.3.4). [Example:
— 仅指针 * 与数组 [] 不同的参数声明是等效的。也就是说,数组声明被调整为指针声明(8.3.5)。只有第二个和随后的数组维度在参数类型 (8.3.4) 中很重要。[例子:
int f(char*);
int f(char[]); // same as f(char*);
int f(char[7]); // same as f(char*);
int f(char[9]); // same as f(char*);
int g(char(*)[10]);
int g(char[5][10]); // same as g(char(*)[10]);
int g(char[7][10]); // same as g(char(*)[10]);
int g(char(*)[20]); // different from g(char(*)[10]);
—end example]
—结束示例]
回答by Tom
There is no difference between your two codes, apart from the different style obviously. In both cases the array is passed by reference and not by value, as function parameters type *x
and type x[]
are semantically the same.
除了风格明显不同之外,您的两个代码之间没有区别。在两种情况下,阵列被通过引用并没有通过由值,作为函数参数type *x
和type x[]
在语义上是相同的。
回答by Patrick
On the style question I'll stick my neck out and say int *arrayOfInt is better. Which ever syntax you use you are passing a pointer and the type should make that clear.
在样式问题上,我会伸出脖子说 int *arrayOfInt 更好。您使用哪种语法传递了一个指针,并且类型应该清楚地表明这一点。
This is just my opinion.
这只是我的看法。
回答by William Bell
The two expressions are equivalent. They each evaluate to the address of the first element of the array arrayOfInt.
这两个表达式是等价的。它们各自计算数组arrayOfInt 的第一个元素的地址。