----- Original Message -----
From: "Supriya" <supriyadv@yahoo.com>
To: <C_Spice@yahoogroups.com>
Sent: Tuesday, January 06, 2004 10:34 AM
Subject: [C_Spice] Please explain this program


>
>
>
> Hi,
>   Here's a small program.

This visibly complex looking program, in reality, is very
simple.  All that you have to understand is following:

    *   Array accesses
    *   Return value of scanf(3)

Before we dig into the details of the program, let us be
clear with the point that array elements can be accessed
in any of the following two ways:

    base[offset]        or      offset[base]

Because, internally the arrays are converted to pointers,
respectively, like this:

    *(base + offset)    or      *(offset + base)

And, you can see that both are the same.

>
>
> #include <stdio.h>
>
> main ()
> {
> int meteor=1;

OK.

>
> printf(&meteor["\021%sin\0not"],(meteor)["have"]+"Link"-0x61);

Divide and rule.  Let us first analyze &meteor["\021%sin\0not"].
What is the sizeof this array "\021%sin\0not"?  Fourteen?  Thirteen?
Wrong... it's ten!  '\021' is just one byte.  See page 38 of
K&R-II.

Assuming ascii 21 is for space, the string (not the array)
becomes " %sin".  &1[" %sin"] gives the address of
'%'; which again is equivalent to calling

    printf ( "%sin", ... );

Now, take the second part: (meteor)["have"]+"Link"-0x61
                              |       |
                               -------
                                 |
     'a' (ascii 0x61) <----------

So, the result is the string "Link".
Upto this point, hence, our output is "Linkin".


>
> meteor=scanf("")+3;

This equivalent to

    meteor = 3;

because scanf(3) returns the number of conversions made; zero
being in our case.

>
> printf (&meteor["TRAP%c%c%c!\n"], 1["Barking up the"],1["wrong"],4
> ["trunk?"]);

Similarly, this printf becomes:

    printf ( "P%c%c%c!\n", 'a', 'r', 'k' );

outputing, "Park".  So, the overall output is:

    "LinkinPark!"


>
> }
>
> The Output of the above program is
>
> LinkinPark!
>
> Please can somebody explain it??
>

You have been explained, just now!

> Regards,
>    Supriya.
>
>