C++ *(Pointer + Index) 和 Pointer[] 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4622461/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:57:56  来源:igfitidea点击:

Difference Between *(Pointer + Index) and Pointer[]

c++pointers

提问by Maxpm

int* myPointer = new int[100];

// ...

int firstValue = *(myPointer + 0);
int secondValue = myPointer[1];

Is there any functional difference between *(myPointer + index)and myPointer[index]? Which is considered better practice?

有没有之间的功能差异*(myPointer + index)myPointer[index]?哪个被认为是更好的做法?

回答by Mike Caron

Functionally, they are identical.

在功能上,它们是相同的。

Semantically, the pointer dereference says "Here's a thing, but I really care about the thing Xspaces over", while the array access says "Here's a bunch of things, I care about the Xthone."

从语义上讲,指针取消引用表示“这是一个东西,但我真的很关心X空间上的东西”,而数组访问则表示“这是一堆东西,我关心Xth一个。”

In most cases, I would prefer the array form.

在大多数情况下,我更喜欢数组形式。

回答by hhafez

There is no difference between

之间没有区别

*(array+10); //and
array[10];

but guess what? since +is commutative

但猜猜怎么了?因为+可交换的

 *(10 + array); //is all the same
 10[array]; //! it's true try it !

回答by paxdiablo

No, they are functionally equivalent.

不,它们在功能上是等效的。

First, indexis scaled up to the type size then added to the myPointerbase, then the value is extracted from that memory location.

首先,index按比例放大到类型大小,然后添加到myPointer基数,然后从该内存位置提取值。

The "better practice" is the more readable one, which is usually, but not necessarily always, the myPointer[index]variant.

“更好的实践”是更具可读性的一种,通常但不一定总是myPointer[index]变体。

That's because you're usually interested in an element of the array, not the memory location to dereference.

那是因为您通常对数组的元素感兴趣,而不是要取消引用的内存位置。

回答by DeveloperChris

There is no functional difference I know of but the form myPointer[1]is ultimately more readable and far less likely to incur coding errors.

我知道没有功能差异,但表单myPointer[1]最终更具可读性,并且发生编码错误的可能性要小得多。

DC

直流电

The form *(myPointer + 1)does not allow for changing the type of pointer to an object and therefore getting access to the overloaded [] operator.

该表单*(myPointer + 1)不允许更改指向对象的指针类型,因此无法访问重载的 [] 运算符。

Also debugging is far harder

调试也更难

 int *ints[10];
 int myint = ints[10]; 

is easier to pickup visually than

比视觉更容易拾取

 int *ints;
 int myint = *(ints + 10); 

also the compiler can insert range checking to catch the error at compile time.

编译器还可以插入范围检查以在编译时捕获错误。

DC

直流电

回答by Tadas ?ubonis

More readable and more maintainable code is better code.

更具可读性和可维护性的代码是更好的代码。

As for functional part... There is no difference. Both times you are "playing with memory".

至于功能部分……没有区别。两次你都在“玩记忆”。

回答by 341008

There is no functional difference. The decision to use either form is usually made depending on the context in which you are using it. Now in this example, the array form is simpler to use and read and hence is the obvious choice. However, suppose you were processing a character array, say, consuming the words in a sentence. Given a pointer to the array you might find it easier to use the second form as in the code snippet below:

没有功能差异。使用任何一种形式的决定通常取决于您使用它的上下文。现在在这个例子中,数组形式更易于使用和阅读,因此是显而易见的选择。但是,假设您正在处理一个字符数组,例如,使用句子中的单词。给定一个指向数组的指针,您可能会发现使用第二种形式更容易,如下面的代码片段所示:

int parse_line(char* line) 
{
    char* p = line;
    while(*p)
    {
         // consume
         p++;
    }
    ...
}

回答by Tushar Gupta

Actually , When an Array 'a' is initialized a pointer to its first memory location ie.. a[0] is returned which is nothing but a ;

实际上,当一个数组 'a' 被初始化时,一个指向它的第一个内存位置的指针即.. a[0] 被返回,它只是 a ;

So if you do 'a+1' it is actually a pointer to a[1]

所以如果你做 'a+1' 它实际上是一个指向 a[1] 的指针

if you do 'a+2' it is actually a pointer to a[2]

如果你做 'a+2' 它实际上是一个指向 a[2] 的指针

if you do 'a+3' it is actually a pointer to a[3] so on ,

如果你做 'a+3' 它实际上是一个指向 a[3] 的指针,依此类推,

so if you do *(a+1) you will get value of a[1] and similar for other values also. if you do *(a) you actually get a[0], So i think its pretty clear now how it works..

所以如果你这样做 *(a+1) 你会得到 a[1] 的值,其他值也类似。如果你这样做 *(a) 你实际上得到了一个 [0],所以我认为它现在很清楚它是如何工作的..