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 1 of 4.

Jetha lal tapuu ke papa said:   3 years ago
I have made it simple.

See;

arr[]={0, 1, 2, 3, 4}
100 104 108 112 116-> address.
arr=base address of an array =100
arr+1=100+1*size of int
100+1*4
arrr +1 = 104
arr+1=108
arr+2=112
arr+3=116
*p[]={100 ,104, 108, 112, 116}
200 204 208 212 216-> address
p =is pointing to base address=200.
p+1= 200+1*size of the pointer.
200+1*4
204
p+2=208
p+3=212
p+4=216
**ptr=p
ptr=200
ptr++ =200+1

in the next line it ptr=204.

Now ptr-p=(ptr-p)/ 4 byte which is the size of a pointer.
(204-200)/4=1,
(104-100)/4=1,
(*(*204))=*104=1.

So, option C is right.
(19)

Laxman said:   7 years ago
@All.

static is the storage class for providing the data secure purpose. in this case static will not effect to the problem but it tells array is stored in data section,we can modify the data what it contain the array. Now come to the solution:

step 1:
static int arr[ ]={0,1,2,3,4};
1) array size is optional during the initialization time, then size depends on a number of elements.
2)assume array starts at the address 1000
then &arr is =1000(this is base address);
&arr[0] (or) arr+0 is =1000
&arr[1] (or) arr+1 is =1004
&arr[2] (or) arr+2 is =1008
&arr[3] (or) arr+3 is =1012
&arr[4] (or) arr+4 is =1016

step 2:
int *p[ ]={arr,arr+1,arr+2,arr+3,arr+4};
1) assume address of p is 2000
2) p is array of pointer type it holds the address as mentioned above, that means
p[0]=arr=1000
p[1]=arr+1=1004
p[2]=arr+2=1008
p[3]=arr+3=1012
p[4]=arr+4=1016

step 3:
int **ptr=p;
1) this means ptr holds the address of p;
2)that means ptr=2000 (we are assuming p address as 2000 above)

step 4:
ptr++;
1)this means ptr=ptr+1 (post increment operator)
2) after this expression ptr holds 2004 (why because one time increment a pointer is 4 bytes thats why 2000+4 )

step 5:
printf("%d, %d, %d\n",ptr-p,*ptr-arr,**ptr);
1)note: in printf multiple expressions are there, then we follow the function data passing
in function data passing expressions are evaluated from right to left but values printed
from left to right. According to this first **ptr is evaluated.
**ptr=*(*(ptr)) //ptr holds 2004 in place of ptr substitute 2004
=*(*(2004)) // dereference of 2004 you get 1004
=*(1004) // dereference of 1004 you get element in that address
=1
and second expression is *ptr-arr;
*ptr-arr=*(ptr)-arr
=*(2004)-arr
=(1004-1000)/4 //note: subtraction of addresses are possible when both are same data t type.
synax is:(address1-address2)/(sizeof data type) in the given expression both are integer (size of int ingcc4bytes)
=1
and third expression is ptr-p;
ptr-p=(2004-2000)/4
=1
now printing from left to right
that means 1,1 1

step 6:
*ptr++;
1)note: in this expression *(dereference) and ++(increment) operators are there. both are unary and same precedence, then we have to consider associativity, all unary operators associativity is from right to left. so in the given expression evaluation stars from right to left. but due to post-increment first *ptr evaluated then ptr++ will evaluate.

So, according to me;
y=*ptr++;
y=*ptr// first *ptr will execute and assign later ptr incremented by one,
ptr=ptr+1;
ptr=2004+(1*4);
ptr=2008;
with this address continue the same procedure as above you get 2, 2, 2 continue the same procedure for all statements.
(5)

Darshan said:   3 years ago
Here in all steps arr and *p are at their base address so in all steps their value will be same i.e =0;.

However value of ptr is incremented by 1 as ++ is present (ie if ++ptr--> ++ptr[0] --> ++0=1;.

So,
in main()
At line 4 ptr++ ----> 0++ ----> 1 then in printf(1-0,1-0,1-0) = 1,1,1;
Line 6 again ptr++ ---> 1++ --> 2 then in printf (2-0,2-0,2-0) = 2,2,2;
(2)

GOWRI said:   6 years 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 the 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.
(2)

Krishu said:   7 years ago
You explained correctly. @Angel_Eyez

And according to me;
So, in step 4 -> ++*ptr ...
NOW, till now, 'ptr' has already become 'ptr+3' i.e --> ptr=ptr+3
AND, this statement ---> " ++(*ptr) " = " ++(*(ptr+3)) "

the point to be noted here is
arr[] = {0, 1, 2, 3, 4};
*p[] = {arr, arr+1, arr+2, arr+3, arr+4};
*p[] = {0, 1, 2, 3, 4};
AND, ptr is pointer to (*p) . SO, any change in ptr will affect changes in *p.

NOW, values after incrementing the values of ptr will become,

[ as per the statements in
++*ptr;
printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
]
arr[] = {0, 1, 2, 3, 4};
*p[] = {1, 2, 3, 4, 5};
and, *ptr[] = {1, 2, 3, 4, 5};

NOW, ptr-p = *(ptr+3)-*p = 4-1 = 3,
*ptr-arr = *(ptr+3)-arr = 4-0 = 4,
**ptr = **(ptr+3) = 4,
(1)

Sri said:   10 years ago
Please give clear explanation.

Sujan said:   1 decade ago
Somewhere I read that subtraction of any pointers gives the result one (1).

Please clarify me about arrays arithmetic operations. Give me reply as soon as possible.

Dileep said:   9 years ago
Can't understand this? Please help me to get it.

Abhishek said:   9 years ago
Thank you guys. Got it finally, special thanks to @Leenaja.

Raji said:   9 years ago
Thank you @Angel_Eyez.


Post your comments here:

Your comments will be displayed after verification.