*(指针+索引)和指针[]之间的区别

时间:2022-11-23 13:25:56
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]之间是否有任何功能差异?哪个被认为是更好的做法?

7 个解决方案

#1


35  

Functionally, they are identical.

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

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

在语义上,指针解除引用说“这是一个东西,但我真的关心X空间的东西”,而数组访问说“这是一堆东西,我关心第十个。”

In most cases, I would prefer the array form.

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

#2


19  

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 !

#3


12  

No, they are functionally equivalent.

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

First, index is scaled up to the type size then added to the myPointer base, then the value is extracted from that memory location.

首先,索引按比例放大到类型大小,然后添加到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.

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

#4


3  

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

#5


1  

More readable and more maintainable code is better code.

更易读,更易维护的代码是更好的代码。

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

功能部分......没有区别。两次你都在“玩弄记忆”。

#6


1  

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++;
    }
    ...
}

#7


0  

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]被返回,这只是一个;

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

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

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

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

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

如果你做'a + 3',它实际上是指向[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],所以我认为它现在非常清楚如何运作..

#1


35  

Functionally, they are identical.

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

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

在语义上,指针解除引用说“这是一个东西,但我真的关心X空间的东西”,而数组访问说“这是一堆东西,我关心第十个。”

In most cases, I would prefer the array form.

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

#2


19  

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 !

#3


12  

No, they are functionally equivalent.

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

First, index is scaled up to the type size then added to the myPointer base, then the value is extracted from that memory location.

首先,索引按比例放大到类型大小,然后添加到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.

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

#4


3  

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

#5


1  

More readable and more maintainable code is better code.

更易读,更易维护的代码是更好的代码。

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

功能部分......没有区别。两次你都在“玩弄记忆”。

#6


1  

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++;
    }
    ...
}

#7


0  

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]被返回,这只是一个;

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

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

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

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

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

如果你做'a + 3',它实际上是指向[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],所以我认为它现在非常清楚如何运作..