[方括号]和*星号的区别 [英] Difference between [square brackets] and *asterisk

查看:32
本文介绍了[方括号]和*星号的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你写了一个 C++ 函数,比如

<前>void readEmStar( int *arrayOfInt ){}

vs 一个 C++ 函数,例如:

<前>void readEmSquare( int arrayOfInt[] ){}

使用 [方括号] 与 *asterisk 之间有什么区别,假设它们等同于编译器,是否有人有关于哪种更可取的样式指南?

为了完整性,举个例子

void readEmStar( int *arrayOfInt, int len ){for( int i = 0 ; i 

解决方案

当你使用类型 char x[] 而不是 char *x 没有初始化,您可以将它们视为相同.您不能在没有初始化的情况下将新类型声明为 char x[],但您可以接受它们作为函数的参数.在这种情况下,它们与指针相同.

当您使用类型 char x[] 而不是 char *x 初始化时,它们完全 100% 不同.<小时>

char x[]char *x 的不同示例:

char sz[] = "你好";char *p = "你好";

sz 实际上是一个数组,而不是一个指针.

assert(sizeof(sz) == 6);断言(sizeof(sz)!= sizeof(char *));断言(sizeof(p) == sizeof(char*));

<小时>

char x[]char *x 相同的示例:

void test1(char *p){断言(sizeof(p) == sizeof(char*));}void test2(char p[]){断言(sizeof(p) == sizeof(char*));}

<小时>

传递给函数的编码风格:

你做哪一个并不重要.有些人更喜欢 char x[] 因为很明显你想要传入的是一个数组,而不是单个元素的地址.

通常这已经很清楚了,因为您将有另一个参数来表示数组的长度.

<小时>

进一步阅读:

请参阅这篇题为 数组与指针不同的帖子!

If you write a C++ function like

void readEmStar( int *arrayOfInt )
{
}

vs a C++ function like:

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

解决方案

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.

When you use the type char x[] instead of char *x with initialization, they are completely 100% different.


Example of how char x[] is different from char *x:

char sz[] = "hello";
char *p = "hello";

sz is actually an array, not a pointer.

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:

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.

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!

这篇关于[方括号]和*星号的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆