Lacoste
Lacoste

Reputation: 31

Two types of matrix output in C++

I tried many many times, in different compilers, but had the same result. My question is:

In C++, given int a[10]; as the declaration of a, then a[0] and 0[a] mean the same. I claim that:

cout << a[0]; 

is the same as

cout << 0[a];

Please explain to me why a[i] is the same as i[a] in C++, where i is an integer.

Upvotes: 0

Views: 128

Answers (2)

Iowa15
Iowa15

Reputation: 3079

Let's say I declare an array: int a[10]; Now a represents some memory address at the beginning of where the array is in memory, and you can think of the index as an offset from that memory address.

So when applying the index operation to the array, the value is defined as follows: a[i] = *(a+i) By the commutative property of addition, you can see that a[0] = 0[a] because of how the index operation is defined by the language.

Upvotes: 0

mohit
mohit

Reputation: 6044

Because array index operations are internally interpreted as *(a+i).

Upvotes: 4

Related Questions