Hi
g.***********@gmail.com wrote:
This program won a "Best One Liner" award in 1987. I found this in
"Communications of the ACM".
Well, Michael gave an explanation of the behaviour.
Notice however that 1987 was quite a while ago and C has changed since then
(C89/90, C99).
I'm trying analyzing the programming trick here.
There is no trick (well, the only one is that "unix" is a macro defined in a
UNIX environment (there, unix == 1))
Try compiling and running on windows...
main() { printf(&unix["\021%six\012\0"], (unix)["have"]+"fun"-0x60);}
As Michael pointed out, a[b] = b[a]. It merely depends which one is the
pointer.
In this case, unix[str] should be read as str[unix], which gives the first
character of str. Therefore &unix["\021%six\012\0"] points to the '%' in
the string, i.e. to the string "%six\012\0". Similarly unix["have"] is 'a',
'a'-0x60 hopefully is 1 (not at all guaranteed) and finally "fun" + 1
points to "un".
hth
Markus