c - With arrays, why is it the case that a[5] == 5[a]? -
as joel points out in stack overflow podcast #34, in c programming language (aka: k & r), there mention of property of arrays in c: a[5] == 5[a]
joel says it's because of pointer arithmetic still don't understand. why a[5] == 5[a]
?
the c standard defines []
operator follows:
a[b] == *(a + b)
therefore a[5]
evaluate to:
*(a + 5)
and 5[a]
evaluate to:
*(5 + a)
a
pointer first element of array. a[5]
value that's 5 elements further a
, same *(a + 5)
, , elementary school math know equal (addition commutative).
Comments
Post a Comment