44
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?

4
  • 35
    You forgot: int thirdValue = 2[myPointer]; Which crazily also works. Commented Jan 7, 2011 at 8:41
  • 2
    @Martin Really? Huh. Learn something new every day, I guess. Commented Jan 7, 2011 at 14:36
  • 3
    @Maxpm - Array subscripting is commutative in C Commented Jun 3, 2013 at 0:42
  • 2
    There is no difference. array[index] and index[array] are just syntax sugar for *(array + index). Commented Aug 17, 2016 at 6:00

8 Answers 8

63

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."

In most cases, I would prefer the array form.

Sign up to request clarification or add additional context in comments.

5 Comments

Someone voted this down? I wish people would comment, and tell me what is wrong with my answer.
Wasn't me, but in this case it's not the Xth but the X+1th.
@Fritschy: Well, as programmers, I assume we count from 0 ;)
Let's not start a flame war, shall we :)
+1 for the semantics, great summary. I'm often tempted to use *(p + offset) whenever I have a pointer, for some idea of consistency (vs 'actual' arrays), but in practical terms I find p[offset] looks better and more intuitive in most cases, relegating the + form to 'offset from this other thing'.
40

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 !

1 Comment

This is true because + is commutative. Operation + is also associative, but this isn't important here.
15

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.

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

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

Comments

6

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.

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.

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

2 Comments

But when you need the address, the form ints + 10 is better than &ints[10].
If you need the address you don't care about overloaded operator[], and avoiding it is safer.
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".

Comments

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

Comments

1

Edit 1 : Decade-old question. But still, I think this answer will help to know the compiler's perspective.

Compiler creates the same machine code for both cases. here's a proof,

code 1

#include<stdio.h>

int main()
{

    int myArr[5] = {1, 2, 3, 4, 5};
    int value = myArr[0];

}

code 2

#include<stdio.h>

int main()
{

    int myArr[5] = {1, 2, 3, 4, 5};
    int value = *(myArr + 0);

}

Below is the result of the comparison done on assembly code generated by compiling the C code of both the codes with gcc -S.

Here is the proof

Comments

-1

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

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

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

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

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.