0

A have found the following snippet:

int a[100];
...
int value = 42[a];

Which appears to do exactly what a[42] does.

Is it a bogus with undefined behavior or a perfectly legal C++ code?

Sergey K.
  • 24,894
  • 13
  • 106
  • 174

4 Answers4

8

It's perfectly legal. With pointer arithmetic, a[42] is equivalent to *(a + 42), which (addition being commutative) is equivalent to *(42+ a), which (by definition of []) is equivalent to 42[a].

So it's obscure, but well-defined.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • Just a small question: is it perfectly legal **in C++** and not just in C, but still works in C++ because legacy ? – JBL Feb 11 '14 at 14:58
  • @JBL I don't see a single mention of C in the question or in my answer. This is perfectly legal C++. – Angew is no longer proud of SO Feb 11 '14 at 14:59
  • Yes I know, but was just wondering. Still I found in §5.2.1/1 what I was looking for (that it's the exact same thing as it's in C). Dunno why I somehow thought it was different... My mistake. – JBL Feb 11 '14 at 15:02
  • @JBL I know why: because it is such a silly language feature that only seems to exist for clever questions and confusion. Even if you know that it came from pointer arithmatic. – stefaanv Feb 11 '14 at 15:17
  • You'll find it regularly used (in C) in the code at http://www.ioccc.org/. – James Kanze Feb 11 '14 at 15:19
  • @stefaanv Erm, I have a small doubt there. It's indeed how the language works that a[i] is an equivalent of *(a+i).It's just that i[a] is a silly side effect of how subscripting works (well, to be really precise, **it's a side effect of addition commutativity** somewhat). I don't think it does exist just for "clever questions" IMO... :/ – JBL Feb 11 '14 at 15:22
  • @JBL sorry, sarcasm (a bit like James' comment). If C/C++ were to be redesigned, it shouldn't be in the language definition because it just adds confusion and questions like this one. Anyway, I thought it was a great reason why you were allowed to doubt whether it was legal in C++. – stefaanv Feb 11 '14 at 16:00
5

The array operator is commutative.

a[n] == *(a + n) == *(n + a) == n[a]

And it's perfectly legal.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • Does that mean ``a`` is just always treated as pointer? – Sergey K. Feb 11 '14 at 15:01
  • 2
    @SergeyK. No. It means that in some operations which are applicable to pointers, but not arrays, `a` is treated as a pointer. In some other operations, such as `sizeof(a)`, or `&a`, it is not. @ᴍaroun ᴍaroun -- No, they are not. (in response to the deleted comment that arrays are constant pointers) – Benjamin Lindley Feb 11 '14 at 15:16
3

a[i] is defined as *(a+i).

So 42[a]=a[42]; and it is perfectly safe

Davidbrcz
  • 2,335
  • 18
  • 27
1

42[a] is exactly equivalent to a[42], and entirely legal. It works because a pointer address is just an integer underneath, so you can do the arithmetic either way round (an array variable is really just a pointer in a thin disguise).

It's not usually a good idea for readability though, unless you're deliberately trying to obfuscate the code.

Peter Bloomfield
  • 5,578
  • 26
  • 37