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

Software Surfer kohli at gemed
Sat Sep 30 04:16:16 AEST 1989


On 29 Sep 89 15:29:00 GMT, Marko Teittinen, student of computer
science, wrote:
>Could someone explain to me what a C compiler does when it runs into
>expression 3["abcdef"]? 
>

Look out-- this was posted earlier this year as one of
several feeble tests of C wizardry, so you'll get a lot
of postings.

The reason 3["abcdef"] is equivalent to "abcdef"[3] is
that "abcdef" is a char*, and 3 is an int.

Expressions of the form: ptr[offset] are evaluated as
ptr+offset, which, you'll note is equivalent to offset+ptr,
which is what you'd expect from offset[ptr].


Jim Kohli



More information about the Comp.lang.c mailing list