I came across this piece of code. In the cout statement the conditions evaluates to true.
a[10][10]=’h’;
cout<<(a[0]==*a)&&(*a==0[a]);
Accessing an array element/address using 0[a] is the new thing.
Can somebody please explain this type of notation ?
From http://c-faq.com/aryptr/joke.html
Q: I came across some joke code containing the expression 5[“abcdef”] . How can this be legal C?
A: Yes, Virginia, array subscripting is commutative in C. [footnote] This curious fact follows from the pointer definition of array subscripting, namely that a[e] is identical to *((a)+(e)), for any two expressions a and e, as long as one of them is a pointer expression and one is integral. The “proof” looks like
This unsuspected commutativity is often mentioned in C texts as if it were something to be proud of, but it finds no useful application outside of the Obfuscated C Contest (see question 20.36).
Since strings in C are arrays of char, the expression “abcdef”[5] is perfectly legal, and evaluates to the character ‘f’. You can think of it as a shorthand for