"abcdef"[3] == 3["abcdef"], but why?

TEITTINEN at cc.helsinki.fi TEITTINEN at cc.helsinki.fi
Sat Sep 30 01:29:00 AEST 1989


As I was trying to figure out what the cryptic maze program (posted to
net some days ago) does, I ran into a very interesting feature of C. The
program exploiting the fact that the following equation is true

         "abcdef"[3] == 3["abcdef"]   (both equal to 'd')

In fact, not only the values are the same. If the string "abcdef" was
replaced by a pointer to char, the expressions would refer to the same
memory location! This can't be a compiler-dependent feature (or bug)
because the maze program runs correctly on various machines.

Could someone explain to me what a C compiler does when it runs into
expression 3["abcdef"]? 

-- 
EARN: teittinen at finuh                        ! "Studying is the only way
Internet: teittinen at cc.helsinki.fi           !  to do nothing without
Marko Teittinen, student of computer science !  anyone blaming you" -me



More information about the Comp.lang.c mailing list