2

Both a[i] and *(a + i) access the i th element of array a.

Are there reasons to prefer one over the other(performance, readability, etc...)?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
locke14
  • 1,335
  • 3
  • 15
  • 36
  • 9
    You can even prefer `i[a]` on top of those two. – Utkan Gezer Feb 19 '14 at 17:01
  • 1
    Whichever is most consistent with the style of the rest of your program. – Hot Licks Feb 19 '14 at 17:02
  • @ShafikYaghmour Neither does yours or any other answer says anything else than `a[i] == *(a+i) == *(i+a) == i[a]` which is actually a FAQ here. Besides, do you have evidence to support which variant _performs_ better than the other. – devnull Feb 19 '14 at 17:19
  • @devnull neither should perform better than the other, array indexing should just be syntactic sugar for pointer arithmetic and as the standard says should be `identical`. If you have evidence otherwise then you should provide an answer that explains the performance difference. I would definitely like to see it but that would be a pretty factual answer wouldn't it? – Shafik Yaghmour Feb 19 '14 at 20:02
  • 1
    It is silly to say this question is opinion based. There is valid and completely factual answer to this question. It is not obvious that one `should` have identical performance unless you go to the standard and see that they should be identical. – Shafik Yaghmour Feb 23 '14 at 01:32

8 Answers8

17

Neither one is better from a language point of view since they are the same, array indexing should just be syntactic sugar for pointer arithmetic. We can see this from the draft C99 standard section 6.5.2.1 Array subscripting which says (emphasis mine):

[...]The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).[..]

Although for others reading your code a[i] is probably more readable.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
7

Although they are identical in arrays, I prefer a[i] for the following reason. Suppose you have code like this:

double gradient[3];

// 200 lines later
double dx = gradient[0]; // Works fine
double dy = *(gradient + 1); // Works just as well

Then somebody decided that std::vector looks better than double[]:

vector<double> gradient;

// 200 lines later
double dx = gradient[0]; // Still works
double dy = *(gradient + 1); // Ouch!

Then somebody else decided to go object-oriented all the way:

class Gradient
{
public:
    double operator[](int index) const; 
};

// 1000 lines later
Gradient gradient;

// 200 lines later
double dx = gradient[0]; // Still works
double dy = *(gradient + 1); // Ouch!!!

Thus, from the viewpoint of maintainability, I prefer to say exactly what I mean, without obscuring the intent with context-dependent synonyms. If I mean "take i-th element of a" then I say exactly that in C++ lingo:

a[i];

rather than relying that for the particular implementation of a one can use a fancier expression for the same.

Michael
  • 5,775
  • 2
  • 34
  • 53
6

a[i] should be preferred, because it is more commonly used and so will be understood more quickly by the person reading the code.

John Bartholomew
  • 6,428
  • 1
  • 30
  • 39
3

C has no operator overloading. So you are safe to use whichever you want.

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
2

Both are equivalent. Neither one is preferred over other but a[i] is commonly used by programmers. a[i] = *(a + i) = i[a]

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
haccks
  • 104,019
  • 25
  • 176
  • 264
2

I would use a[i] for clarity when reading the code.

Simon Karlsson
  • 4,090
  • 22
  • 39
1

As far as I'm concerned (just finished a C course), there is no difference.

Array is implemented as a pointer to the first element on the stack. So a+1 is just the address on the stack after a :)

Edit: Also, as mentioned by John Bartholomew, even though they are the same, you might want to use a[i] instead - that is the "normal" way to do it (also in other languages) :)

Nicolai Krüger
  • 416
  • 4
  • 17
1

For readability purposes, I would definitely go with a[i].