+ 3
What does a[1] means...?
if it means base a + 1 as index then a[1] and 1[a] should be same. main () { int a[2] = {1,2}; cout << a[0]<<1[a]; }
7 odpowiedzi
+ 8
a [1] means the second value of an array as array's index starts with zero .
+ 6
Dennis
Good point. If a[1] == *(a + 1), and 1[a] == *(1 + a), and since the addition is commutative ¹
, then a[1] == 1[a].
____
¹
https://en.m.wikipedia.org/wiki/Commutative_property
+ 5
The number between the [] is just an offset from the base address.
Unless the [] operator is overloaded to do something else for some reason...
For example if the array a is located at memory address
0x22FE48 ( the base address ) and assuming an int being 4 bytes, then
a[0] = 0x22FE48 + ( 4 * 0 ) = 0x22FE48,
a[1] = 0x22FE48 + ( 4 * 1 ) = 0x22FE4C,
a[2] = 0x22FE48 + ( 4 * 2 ) = 0x22FE50,
... etc
0[a], 1[a]... etc are the same as a[0], a[1]... etc. At least for c-style arrays.
It's NOT an error.
Stuff like this also allows things like "abcd"[2] = 'c', for example.
+ 2
array index logic
+ 2
good question
+ 2
'a' its an adress in that sense, and adress use [] operator opposite of int
0
check out
https://code.sololearn.com/c3am0SO5xL11/?ref=app
and you will have an idea.
Eduardo Petry