0

Please explain this code

Please I saw this code on community challenge and I got curious so I run it on playground. The correct answer on the challenge was 83 e but I got a different answer here (115 e). Please who can explain this code bit Thanks https://code.sololearn.com/caeChPu6zEs4/?ref=app

7th Dec 2020, 12:35 PM
Sudo
Sudo - avatar
9 Respuestas
+ 2
Brian thanks It'd be great if they add such tutorials here
7th Dec 2020, 1:37 PM
Sudo
Sudo - avatar
+ 1
0[arr] returns first element ‘s’ whose ascii value is 115. 2[arr+3] means 3rd character from ‘o’ i.e. ‘e’.
7th Dec 2020, 12:58 PM
Flash
+ 1
Chidera the array syntax 0[arr] and 2[arr+3] is an unconventional way of expressing the equivalent expressions arr[0] and arr[2+3]. Array expressions are composed of a memory address and an offset, like x[i], where x is the address and i is the offset. The offset i gets added to x to calculate the element address. The final element address is x+i. Because addition is commutative, you get the same address result whether you add x+i or i+x. Therefore, both x[i] and i[x] are the same. The latter, i[x], tells C to start with address i (which is really an index) and add offset of x (which is really an address). [EDIT: Remember that this is pointer arithmetic so, when adding the offset, it adds offset*(sizeof <type>).]
7th Dec 2020, 1:33 PM
Brian
Brian - avatar
0
Pąľľąvī thanks but still confused 😐
7th Dec 2020, 12:51 PM
Sudo
Sudo - avatar
0
Why is the array arr used like this 0[arr] and 2[arr+3]?
7th Dec 2020, 12:55 PM
Sudo
Sudo - avatar
0
Thanks Pąľľąvī and Flash
7th Dec 2020, 1:26 PM
Sudo
Sudo - avatar