Interesting C thingy

Carlos Martin carlosmn at gmail.com
Mon Aug 9 09:39:38 PDT 2004


On Mon, 09 Aug 2004 09:14:39 -0700, Kevin P. Fleming
<kpfleming at linuxfromscratch.org> wrote:
> Jason Gurtz wrote:
> 
> > This came up on another list:
> >
> > What does printf("%c\n",0["unix"]); output and why?

It will print "u\n".
The reason is that 0["unix"] is equivalent to saying "unix"[0] where
unix would be later
changed into a pointer which would hold this address, thus it would
become something
to the effect of:

char* str = "unix";
printf(%c\n", str[0]);

which would pass 'u' to printf().

It is equivalent because 0["unix"] is equal to ("unix" + 0) which
would also equal
("unix" + 0).
In all of these cases, "unix" should/would be changed into a pointer
which holds the
address of the string, as in the code example above.

Or maybe I'm just going crazy, who knows...
-- 
Carlos Martín
SoKrA-BTS



More information about the lfs-chat mailing list