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

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

问题描述

如果你写像

C ++函数


无效readEmStar为(int * arrayOfInt)
{
}

VS一个C ++类的函数:


无效readEmSquare(INT arrayOfInt [])
{
}

有什么用[方括号] VS *星号之间的差异,并没有任何人有一个风格指南,以这是preferrable,假设他们是等价的编译器?

有关完整性,一个例子

 无效readEmStar为(int * arrayOfInt,INT LEN)
{
  的for(int i = 0; I< LEN,我++)
    的printf(%d个,arrayOfInt [I]);
  卖出期权();
}
无效readEmSquare(INT arrayOfInt [],INT LEN)
{
  的for(int i = 0; I< LEN,我++)
    的printf(%d个,arrayOfInt [I]);
  卖出期权();
}诠释的main()
{
  INT R [] = {2,5,8,0,22,5};  readEmStar(R,6);
  readEmSquare(R,6);
}


解决方案

当您使用类型字符X [] 而不是的char * X 不用初始化,你可以认为它们是相同的。你不能声明一个新的类型字符X [] 不用初始化,但是你能接受它们作为函数的参数。在这种情况下,它们是相同的指针。

当您使用类型字符X [] 而不是的char * X 与初始化,他们是完全100%的不同。


如何示例焦X [] 不同于的char * X

 字符SZ [] =你好;
的char * p =你好;

SZ 实际上是一个数组,而不是一个指针。

 断言(的sizeof(SZ)== 6);
断言(的sizeof(SZ)= sizeof的(字符*)!);
断言(的sizeof(P)==的sizeof(字符*));


如何示例焦X [] 相同的char * X

 无效test1的(字符* P)
{
  断言(的sizeof(P)==的sizeof(字符*));
}空TEST2(字符P [])
{
  断言(的sizeof(P)==的sizeof(字符*));
}


以传递给函数的编码方式:

这真的不要紧,你做哪一个。有些人preFER 字符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天全站免登陆