C Programming - Arrays - Discussion

Discussion Forum : Arrays - Find Output of Program (Q.No. 5)
5.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    static int arr[] = {0, 1, 2, 3, 4};
    int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
    int **ptr=p;
    ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *++ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    ++*ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    return 0;
}
0, 0, 0
1, 1, 1
2, 2, 2
3, 3, 3
1, 1, 2
2, 2, 3
3, 3, 4
4, 4, 1
1, 1, 1
2, 2, 2
3, 3, 3
3, 4, 4
0, 1, 2
1, 2, 3
2, 3, 4
3, 4, 5
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
38 comments Page 3 of 4.

Pragna said:   1 decade ago
arr[] sotres the elements 0,1,2,3,4
arr , arr+1 ,arr+2... represents the base address 32--0 ,33--1 ,34--2 ...

*p[]={ base address are sotred.}

Here p(48) also represents a base address . p base address 48 (something not in array) stored in ptr.

*ptr(32) gives base address = = arr.

ptr++ increments (49)
--> ptr(49) - p(48) = 1.
-->*ptr(33) - arr(32) =1.
-->*(*ptr) gives the value 1.(bcz *(33) = 1.)

ptr=49
*ptr++ increments (34)
--> ptr(50) - p(48) = 2.
-->*ptr(34) - arr(32) =2 , arr(address) same.
-->**ptr gives the value 2.(bcz *(34)=2)

ptr=50
*++ptr increments (35) Although *ptr++ = *++ptr
-->ptr(51) - p(48) = 3.
-->*ptr(35) - arr(32) = 3.
-->**ptr gives the value 3.(bcz *(35)=3)

ptr=51 (it will not change down bcz it is not incremented.)
++*ptr (36)
But ptr = 51
--> ptr(51) - p(48) = 3.
-->*ptr(36) - arr(32) = 4.
-->**ptr gives the value = 4.(bcz *(36)=4.)

So the result
1 1 1
2 2 2
3 3 3
3 4 4

Leenaja said:   1 decade ago
i am trying to make this simpler.

Here p[] is a pointer array to arr[]
and ptr is pointer variable to p[].

1.ptr++;

Initially ptr is at 0 position referring to *p[0], now ptr is incremented and is at 1st position *p[1].

Initially p is at 0

so ptr -p= 1-0 =0
*ptr is the value present in *p[1] i.e, arr+1 hence *ptr-arr=1
**ptr is the value present in arr at position 1 i.e 1
2. *ptr++
the same thing happens here. ptr is again incremented by 1. now it points to *p[2].
ptr-p=2-0=0
*ptr is value present in *p[2] i.e, arr+2 and *ptr-arr=2
**ptr is value present in arr at position 2 i.e 2.
3. *++Ptr
this is same as 2. ptr is incremented and it points to *p[3]
so the remaining is same
4. ++*ptr
in case 2 and 3 pointer is incremented but here the value that the ptr pointing is incremented

ptr is pointing to arr+3 in *p[] and arr+3 is incremented
so *p[3]= arr+4;

ptr-p= 3-0=3
*ptr-arr = arr+4-arr=4
**ptr refers to the value in arr+4 i.e arr[4]=4

Khurram Baig said:   1 decade ago
4th step is explained wrongly.

++*ptr mean value pointed by ptr is being incremented. So it increments arr+3 or arr[2].

Vik said:   1 decade ago
@Angel eyez thanks.

Jitendra said:   1 decade ago
Where give ptr is point to a pointer ?

Neeraj said:   1 decade ago
@anand, @vedavati, @karthiga

If you urself can't understand then don't try to explain the answer here, it will confuse others, thank you.

Nirav said:   1 decade ago
*Great Angel_Eyez...Thanks..

Karthick said:   1 decade ago
Step 4 confused please help.

Nagesh said:   1 decade ago
I also confused man some body help.

Angel_eyez said:   1 decade ago
p is an array of pointers. ptr is a pointer to a pointer, initailly referring to p.
Step 1:
ptr++
ptr is incremented by 1, i.e. ptr now points to p+1 or (arr+1)
ptr-p=1, *ptr=arr+1, so *ptr-arr=1, **ptr=*(arr+1)=1
Step 2:
*ptr++
is *(ptr++), i.e. ptr=ptr+1=arr+2, the * part is not of any work here.
ptr-p=2, *ptr=arr+2, so *ptr-arr=2, **ptr=*(arr+2)=2
Step 3:
*++ptr
Similar to step 2, *++ptr=*(++ptr)
ptr-p=3, *ptr=arr+3, so *ptr-arr=3, **ptr=*(arr+3)=3
Step 4:
++*ptr
is ++(*ptr), i.e. ptr now points to arr+4, but point to be noted that *p also changes,since ptr is pointer to p. Here ptr does not change its reference position unlike all the other steps, but changes the value it is pointing to
*p[]={arr, arr+1, arr+2, arr+4, arr+4}
so, ptr-p=3, *ptr=p[3]=arr+4, so *ptr-arr=4, **ptr=*(arr+4)=4
Hope this helps


Post your comments here:

Your comments will be displayed after verification.