2

Can anyone explain me why this is possible ?. I understand that a is the same as &a[0] and for example i also understand that *(a+1) is second a element and i am able to use *((a+i)+j) techniques, but first time in my life i saw code like this and i am confused now. I know that output is fourth element of the a array. But can anyone explain me why this is possible ?

#include<iostream>
int a[] = {1,2,3,4,5};
std::cout << (3)[a] << std::endl; 
haccks
  • 104,019
  • 25
  • 176
  • 264
user2667455
  • 99
  • 2
  • 9
  • Important note that may save you some future confusion: `a` is not the same as `&a[0]`. It can be used the same way, but can also be used a number of different ways. For example `sizeof(a)` will return the size of the array in bytes. `sizeof( &a[0])` will return the size of a pointer. – user4581301 May 18 '16 at 07:04

1 Answers1

10

The binary expression:

x[y]

is defined to be equal to:

*(x+y)

(in the absence of operator [] overloads).

3+a is the same as a+3 so 3[a] is the same as a[3].

In the context of 3+a, the name of array a decays to a pointer to its first element, so we are trying to add a pointer and an integer using the builtin addition operator. The latest draft of C++14 says in section 5.7 [expr.add] para 4:

When an expression that has integral type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integral expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i + n-th and i − n-th elements of the array object, provided they exist

This is very close to the text of the C++14 standard, but this behaviour has been stable since C89 (and actually since BCPL in the '70s).

Having said that, if you find code like this hunt down the author and beat him over the head with a clue-bat. It is very easy to write C++ that nobody (including yourself after a week) can read - and that is no use to anyone.

  • Could You explain please why 3+a is the same as a+3 ? Is here working default + operator which is defined as for example: const int& operator+(const int& n, int []arr) and the same moment there is operator + defined: const int& operator+(int[] arr, const int& n) ? Is my understanding good for this step ? – user2667455 May 18 '16 at 06:53
  • Standard math. 10+3 = 3+10. Only difference here is we're using an address instead of 10. Adding n to a pointer moves you forward n elements as if the pointer was an array just like `pointer[n]` would. – user4581301 May 18 '16 at 06:58
  • @user2667455 : The addition operator is defined for both pointer+int and int+pointer (and means the same thing). I have added a quote from the standard to clarify. – Martin Bonner supports Monica May 18 '16 at 07:04
  • 1
    @user4581301: The standards committee didn't *have* to define `a+3` to be the same as `3+a`. They could have chosen to only define the first. I certainly wish they had only defined `a[3]`. – Martin Bonner supports Monica May 18 '16 at 07:07
  • THANK YOU I FINALLY UNDERSTAND THIS!!!!!!!!!!! – user2667455 May 18 '16 at 07:08
  • They didn't, but to keep in line with arithmetic, I'd keep `a+3` and `3+a`. `3[a]` however.... That's just freaking weird. – user4581301 May 18 '16 at 07:28