Hello,

Im looking at this weird little code fragment I stumbled upon the other day
and I cant figure out how it works.
OK, I get the picture how the array is filled up to the fourth index, but that descending part is evil. And weird. :)

#include<stdio.h>
int main(void)
{
    int p[10];
    int i;
    for(i=0;i<10;i++)
    {
        p[i]=9-i;
        printf("%d",p[i]);
        }
    printf("\n");
    for(i=0;i<10;i++)
    {
        p[i]=p[p[i]];
        printf("%d",p[i]);
        }
    getchar();
    return 0;
}

What line(s) are you having problems with?

Its basically this little array filler :

p[i]=p[p[i]];

I'm guessing its the p[p] part. Well its simple if you break it down..

We'll look at the inner p which just returns the i-th element of the array. The value from the inner p is used as the 'index value' of the outer p[index value]. Its a little unorthodox to use an array this way.

Ooops I double posted by accident...Can a moderator remove this one..Thanks.

Yeah, I reckon it is. Anyways thank u for your help m8, much appreciated.
Cheers:)

i think u need to modify it
p=9-i;
printf("%d",p);
u can use another varible for printing that.
________________
click here.

Woo. what is this thing? Some kind of clue to puzzle or is it a array filler

_________________________________________
Barhocker : Hier finden Sie eine sehr große Auswahl an Barhocker Internationaler Versand oder Abholung in Berlin.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.