3

I'm trying to understand the output of the following piece of code, how does it produces an output

-2 1 2 -4 3

int main()
{
    int i, a[5] = {3, 1, 2, -2, -4};
    int *p = a;

    for(i = 0; i < 5; i++)
    {
        printf("%d ", *(p + *p));
        p += *p;
    }

    return 0;
}
1

5 Answers 5

3
a[0]   a[1]  a[2]  a[3]  a[4]   // array indexes
 3       1     2    -2    -4    // value
1001   1005   1009  1013  1017  // Consider it is memory location.

p == 1001; // p=a;

Incrementation will done like this. p+ (value * sizeof(int) )

In first loop,

1001 + 3 ; // *(p + *p); p== 1001 , *p == 3

So it will print the value placed in the 1013 that is equal to -2.

After that you are doing this `p += *p;` Now `p` points to `1013`

Second loop,

1013 + -2; // *(p + *p); p== 1013 , *p == -2

So it will print 1 (1005 ). After that assigning , now p points to 1005.

Third loop,

1005 + 1 ; // *(p + *p ); p == 1005  , *p == 1

It will print 2(1009). Now p changes into 1009.

Fourth loop,

1009 + 2 ;  // *(p + *p ); p == 1003 , *p == 2

It will print -4(1017) and p changes into 1017.

At finally,

1017 + -4 ; // It becomes 1001.

So It will print the value in 1001. so the value is 3.

Sign up to request clarification or add additional context in comments.

Comments

2

This program is mainly based on pointer arithmetic.

  • p is the pointer.
  • *p is the value at the pointer
  • *(p + *p) is effectively p[*p]
  • p += *p; is p = p + *p

Now, based on the particular values, in this case, for first iteration,

printf("%d ", *(p + *p));

is

printf("%d ", *(p + 3));

or printf("%d ", p[3]);

which gives -2.

Next, p += *p is p += 3 and so on.

You can read more about pointer arithmatic here.

Comments

1
*(p + *p) = *(p + 3) = p[3] = -2 /* p = a and *p will give the value of first element of the array */

p = p + *p = p + 3

*(p + *p) = *((p+3) + *(p+3)) = *((p+3) + p[3]) = *(p + 3 -2) = p[1] = 1

and so on

PS: a[i] = *(a+i)

Comments

1

Consider this condition... n some byte address

 Elements:       3  1    2   -2      4
 with addresses:  n  n+1  n+2  n+3  n+4 (this is in bytes)

Now initially we have *p=a that is p points the first element of array a. So, we would be having *p=3 and p=n(i.e. p has address of first element which is n) .

In the first loop we have .. printf("%d ", *(p + *p));....

So, *(p + *p) implies element at address (p+*p) where we have p=n and *p=3 . So *(p + *p) is the element at n+3 that is -2.

No we have p += *p; . Which means p=p+ *p . SO p = n + 3 ==> p now points to the -2 ..

This is how it loops..

Loop 1:
   *p = 3
    p = n
    *(p + *p ) ==> *(n+3) = -2
    p = p + *p ==>  p = n+3
Loop 2:
    *p = -2
     p = n+3
    *(p + *p ) ==> *(n+3 -2) = 1
     p =p + *p ==> p = n+3 -2 ==> n+1 
Loop 3:
    *p = 1
     p= n+1
    *(p + *p ) ==> *(n+1 +1) = 2
     p =p + *p ==> p = n+1 +1 ==> n+2
Loop 4:
    *p = 2
     p= n+2
    *(p + *p ) ==> *(n+2 +2) = -4
     p =p + *p ==> p = n+2 +2 ==> n+4
Loop 5:
    *p = -4
     p= n+4
    *(p + *p ) ==> *(n+4 -4) = 3
     p =p + *p ==> p = n+4 -4 ==> n

Comments

1
value  3   1    2   -2   -4
addr 100  104  108  112  116  (say)

First iteration:
------------------------------------------------------------------
int *p = a; --> p = 100 (base address of the array )
( *(p + *p) ) --> (*(100+4(size of int)X *(100) ) --> (*(100+4X3)) -->(*(112))= -2

 p += *p; -->(p= (p+*p))-->(100 +*(100)X4) --> (100 +3X4) --> (112)
Now p=112 (memory address).

Second iteration:
----------------------------------------------------------------------
( *(p + *p) )-->(*(112+*(112)X4)-->*(112 (-2X4))-->*(112-8)=*(104)=1
p += *p; -->(p= (p+*p))-->((112 + *(112)X4)-->(112 +(-2X4))= 104
now p=104 (memory address)


like this ......

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.