[方括号]与*星号之间的差异

时间:2021-03-14 21:41:32

If you write a C++ function like

如果你写一个c++函数

void readEmStar( int *arrayOfInt )
{
}

vs a C++ function like:

与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 ) ;
}

5 个解决方案

#1


59  

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类型时,它们是完全不同的。


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 *[]与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!

请查看这篇题为“数组与指针不同”的文章!

#2


13  

C++ Standard 13.1.3

13.1.3 c++标准

— 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]

端例子)

#3


3  

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.

你们两个代码之间没有区别,除了明显不同的风格。在这两种情况下,数组都是通过引用传递的,而不是通过值传递的,因为函数参数类型*x和类型x[]在语义上是相同的。

#4


1  

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.

这只是我的看法。

#5


-1  

The two expressions are equivalent. They each evaluate to the address of the first element of the array arrayOfInt.

这两个表达式是等价的。它们各自计算数组arrayOfInt的第一个元素的地址。

#1


59  

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类型时,它们是完全不同的。


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 *[]与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!

请查看这篇题为“数组与指针不同”的文章!

#2


13  

C++ Standard 13.1.3

13.1.3 c++标准

— 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]

端例子)

#3


3  

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.

你们两个代码之间没有区别,除了明显不同的风格。在这两种情况下,数组都是通过引用传递的,而不是通过值传递的,因为函数参数类型*x和类型x[]在语义上是相同的。

#4


1  

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.

这只是我的看法。

#5


-1  

The two expressions are equivalent. They each evaluate to the address of the first element of the array arrayOfInt.

这两个表达式是等价的。它们各自计算数组arrayOfInt的第一个元素的地址。