Sunday, 6 September 2015

With C arrays why is a[n]==n[a]

With C arrays why is it the case that  a[5] is equal to 5[a].


The C standard, defines the operator [ ] as follows:
   a[b] == *(a+b)


So, a[5] will evaluate to *(a+5) and 5[a] will evaluate to *(5+a).
Arrays are essentially the pointers a is the memory location and a[5] is the value of element five elements away from a.This is equal to offset "a" from "5" elements at the beginning of the address space (5+a). 




And since the addition is cumulative they both are same.


To see more such questions click the link below:More Questions

No comments:

Post a Comment