+ 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]; }

24th Aug 2018, 7:33 PM
Siddharth Yadav
Siddharth Yadav - avatar
7 odpowiedzi
+ 8
a [1] means the second value of an array as array's index starts with zero .
26th Aug 2018, 4:22 PM
Purab Gupta
Purab Gupta - avatar
+ 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
24th Aug 2018, 9:33 PM
Babak
Babak - avatar
+ 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.
24th Aug 2018, 8:32 PM
Dennis
Dennis - avatar
+ 2
array index logic
24th Aug 2018, 7:34 PM
Siddharth Yadav
Siddharth Yadav - avatar
+ 2
good question
24th Aug 2018, 7:37 PM
Pujan Shah
Pujan Shah - avatar
+ 2
'a' its an adress in that sense, and adress use [] operator opposite of int
24th Aug 2018, 7:41 PM
KrOW
KrOW - avatar
24th Aug 2018, 7:56 PM
Siddharth Yadav
Siddharth Yadav - avatar