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;
}
Discussion:
38 comments Page 1 of 4.
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.
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)
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
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
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.
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)
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
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
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,
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)
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
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
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.
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)
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;
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)
Karthiga said:
1 decade ago
p[]=(0,1,2,3,4);
ptr is point to pointer operation which will increament index position by one.
So , Now its point to 1. So ptr 1 and p 0
So for first printf statement ptr-p, *ptr-arr, **ptr => 1-0,1-0,1
Same way fellow for next by increment ptr where *ptr and ptr give the value in the index postion.
ptr is point to pointer operation which will increament index position by one.
So , Now its point to 1. So ptr 1 and p 0
So for first printf statement ptr-p, *ptr-arr, **ptr => 1-0,1-0,1
Same way fellow for next by increment ptr where *ptr and ptr give the value in the index postion.
Vedavathi said:
2 decades ago
Here we will identify the answer depending on **ptr. At first **ptr points to first element i.e '0'. After incrementind ptr++ that address 2nd element. So in the options 'C' option have '1'. So we guess 'c' as the answer.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers