Explain the output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
15th Mar 2020, 2:58 PM
Sachin Shah
Sachin Shah - avatar
3 Answers
0
a[i] is evaluated as *(a + i) so a[2] = *(a + 2) 2[a] = *(2 + a) = *(a +2) in other words 2[a] referes to the same element as a[2]. a[2] = 1; 2[a] is also = 1 so output will be 1.
15th Mar 2020, 3:10 PM
andriy kan
andriy kan - avatar
0
the compiler converts the array operation in pointers before accessing the array elements, which means all of the following expressions mean same thing *(a+2), a[2], 2[a]: https://code.sololearn.com/cUE4lD7fokb9/#c BTW not sure why you have the statement a[-3]=10; but it's probably not doing what you think it's doing. specifically, the compiler will, almost definitely be able to establish statically that this is undefined behavior and, being undefined behavior, could choose to do just about anything with it; complete removal being the most likely.
15th Mar 2020, 4:11 PM
MO ELomari
15th Mar 2020, 4:17 PM
Sachin Shah
Sachin Shah - avatar