C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 13)
13.
What will be the output of the program?
#include<stdio.h>
int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p+1);
printf("%d", *p);
return 0;
}
Discussion:
87 comments Page 1 of 9.
Chetan Agarwal said:
10 years ago
It all depends on compiler as well.
First p = (int *)(p+1); is a error, reason is that p is char pointer so if some of you guys are getting value of *p its because it didn't actually converted to int pointer, your compiler failed to convert and instead of raising error it just raised warning.
I tried in Turbo C and my observation is if array starts from 1000.
1000 -> 2.
1001 -> 0.
1002 -> 3.
1003 -> 0.
1004 -> 4.
Now, this statement p = (int*)(p+1) in the problem should have been written as:
1. p = p+1;
//Now p starts from 1001.
OR.
2. int*q = (int *)(p+1);
//Now q starts from 1001.
Consider Case 1: printf ("%d", *p); //Output: 0.
Consider Case 2: printf ("%d", *q); //Output: 768.
Reason: q is an int pointer so it has to access 2 bytes.
Remember this is little endian architecture so, 1002 byte address will get accessed first and then 1001.
Therefore the value would be represented in q as following.
00000011 00000000.
1002 1001.
This amounts to 768.
Note: If in your system int is 4 bytes then value would be different but it won't be 0.
And those who are saying that p = (int *)(p+1); works, p is not actually getting converted to int pointer, so the answer is of no significance.
First p = (int *)(p+1); is a error, reason is that p is char pointer so if some of you guys are getting value of *p its because it didn't actually converted to int pointer, your compiler failed to convert and instead of raising error it just raised warning.
I tried in Turbo C and my observation is if array starts from 1000.
1000 -> 2.
1001 -> 0.
1002 -> 3.
1003 -> 0.
1004 -> 4.
Now, this statement p = (int*)(p+1) in the problem should have been written as:
1. p = p+1;
//Now p starts from 1001.
OR.
2. int*q = (int *)(p+1);
//Now q starts from 1001.
Consider Case 1: printf ("%d", *p); //Output: 0.
Consider Case 2: printf ("%d", *q); //Output: 768.
Reason: q is an int pointer so it has to access 2 bytes.
Remember this is little endian architecture so, 1002 byte address will get accessed first and then 1001.
Therefore the value would be represented in q as following.
00000011 00000000.
1002 1001.
This amounts to 768.
Note: If in your system int is 4 bytes then value would be different but it won't be 0.
And those who are saying that p = (int *)(p+1); works, p is not actually getting converted to int pointer, so the answer is of no significance.
(2)
Puru said:
1 decade ago
p = (char*)((int*)(p));
// till now the pointer p is type casted to store the variable of type character.
printf("%d, ", *p); // %d means integer value so value at first address i.e. 2 will be printed.
p = (int*)(p+1); // here p is still of type character as type casted in step 1 so p(i.e address) and plus 1 will increase only by one byte so
as per viraj suggested
Assuming that integer requires 2 bytes of storage
the integer array will be stored in memory as
value 2 3 4
address 00000010 00000000 00000011 00000000 00000100 00000000
pointer p+1
so p+1 points to that location which is unfilled as during intialization 2,3,4 were stored in variable of type integer(2 bytes).
so p+1 will point to 00000000.
(int*)p+1 // p+1 is type casted again to integer
printf("%d", *p); // this will print 0 as output as by default integer contains 0 as value.
// till now the pointer p is type casted to store the variable of type character.
printf("%d, ", *p); // %d means integer value so value at first address i.e. 2 will be printed.
p = (int*)(p+1); // here p is still of type character as type casted in step 1 so p(i.e address) and plus 1 will increase only by one byte so
as per viraj suggested
Assuming that integer requires 2 bytes of storage
the integer array will be stored in memory as
value 2 3 4
address 00000010 00000000 00000011 00000000 00000100 00000000
pointer p+1
so p+1 points to that location which is unfilled as during intialization 2,3,4 were stored in variable of type integer(2 bytes).
so p+1 will point to 00000000.
(int*)p+1 // p+1 is type casted again to integer
printf("%d", *p); // this will print 0 as output as by default integer contains 0 as value.
Noel Nosse said:
7 years ago
Output is 0 except for { p = (int*)(p+0) }, { p = (int*)(p+4) } and { p = (int*)(p+8) }.
Per the previous question in is FOUR BYTES, so the binary saved in memory in NOT as above, but:
00000000 00000000 00000000 00000010 - first 4 bytes for 2
00000000 00000000 00000000 00000011 - second 4 bytes for 3
00000000 00000000 00000000 00000100 - third 4 bytes for 4.
The { p = (int*)(p+1); } is moving us ONLY 8 bites (one byte) at a time - AFTER STARTING with the "2" at the end of the first bytes. "p+2" carries us only 8 bits forward because of { char *p;...p = (char*)((int*)(p)); }.
Throughout p is a CHAR pointer moving only the 1 byte of a char.
Per the previous question in is FOUR BYTES, so the binary saved in memory in NOT as above, but:
00000000 00000000 00000000 00000010 - first 4 bytes for 2
00000000 00000000 00000000 00000011 - second 4 bytes for 3
00000000 00000000 00000000 00000100 - third 4 bytes for 4.
The { p = (int*)(p+1); } is moving us ONLY 8 bites (one byte) at a time - AFTER STARTING with the "2" at the end of the first bytes. "p+2" carries us only 8 bits forward because of { char *p;...p = (char*)((int*)(p)); }.
Throughout p is a CHAR pointer moving only the 1 byte of a char.
(2)
Poornachandra h d said:
8 years ago
@Shivam Varshney : the answer is correct.
p=(char *)((int *)(p)); and p=(char *)(p) ; //both are same
so, let us consider starting address of p is 1000;
when we load p = (char *)p or p = (char *)((int *)(p));
the digit '2' is stored as character in 'p'. And next three bits are left as zero;
when you increment address by +1 execute, we get '0'.//now p is 1001
in the compiler, try by increasing 'p = p+4' and print --- you will get 3 //now p is 1004
(now you try printing p+1)//i.e, 1005 --- you will get zero
and again do 'p = p + 4' //now it is 1008.
what will it print now?
Yes, exactly next digit which is stored as char '4'.
Thank you!
p=(char *)((int *)(p)); and p=(char *)(p) ; //both are same
so, let us consider starting address of p is 1000;
when we load p = (char *)p or p = (char *)((int *)(p));
the digit '2' is stored as character in 'p'. And next three bits are left as zero;
when you increment address by +1 execute, we get '0'.//now p is 1001
in the compiler, try by increasing 'p = p+4' and print --- you will get 3 //now p is 1004
(now you try printing p+1)//i.e, 1005 --- you will get zero
and again do 'p = p + 4' //now it is 1008.
what will it print now?
Yes, exactly next digit which is stored as char '4'.
Thank you!
Veena said:
1 decade ago
#include<stdio.h>
int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
printf("\n address pointed by p%u",p);
p = (int*)(p+1);
printf("\n%d", *p);
printf("\n address pointed by p%u",p);
p++;
printf("\n%d", *p);
printf("\n address pointed by p%u",p);
return 0;
}
Output:
2.
Address pointed by p 2686704.
0.
Address pointed by p 2686705.
0.
Address pointed by p 2686706 // after int conversion why it only increment only one byte m/r location. And why not printing 3.
int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
printf("\n address pointed by p%u",p);
p = (int*)(p+1);
printf("\n%d", *p);
printf("\n address pointed by p%u",p);
p++;
printf("\n%d", *p);
printf("\n address pointed by p%u",p);
return 0;
}
Output:
2.
Address pointed by p 2686704.
0.
Address pointed by p 2686705.
0.
Address pointed by p 2686706 // after int conversion why it only increment only one byte m/r location. And why not printing 3.
Mayur22kar said:
4 years ago
@All.
According to me; the coding part is;
#include<stdio.h>
int main()
{
// create and array hear with name arr
int arr[3] = {2, 3, 4};
// assume array is created at memory location 1000
// so 2 is store at location 1000 and
// 3 on 1004 and
// 4 is store at 1008......
// created one char pointer *p
char *p;
//p is pointing to the 1000
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p+1); // when you increase hear p+1 then 1000 becomes 1001 because p is character pointer
printf("%d", *p);
return 0;
}
According to me; the coding part is;
#include<stdio.h>
int main()
{
// create and array hear with name arr
int arr[3] = {2, 3, 4};
// assume array is created at memory location 1000
// so 2 is store at location 1000 and
// 3 on 1004 and
// 4 is store at 1008......
// created one char pointer *p
char *p;
//p is pointing to the 1000
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p+1); // when you increase hear p+1 then 1000 becomes 1001 because p is character pointer
printf("%d", *p);
return 0;
}
(11)
Balu said:
1 decade ago
Assume ,
arr[0] = 1000.
arr[1] = 1004.
arr[2] = 1008.
now,
p=arr; i.e p=1000.
p = (char*)((int*)(p));
At this statement character pointer p 1st typecasted to integer pointer and again typecasted to character pointer.
printf("%d, ", *p);-prints value 2.
As memory representation for little endian (lowest byte will stored 1st).
00000010 00000000 00000000 00000000
p = (int*)(p+1);-in this statement p which is char pointer is incremented by one so it is pointing at location 1001.
printf("%d", *p);-prints value 0 because at memory location 1001 value 0 is stored (according to little endian ).
arr[0] = 1000.
arr[1] = 1004.
arr[2] = 1008.
now,
p=arr; i.e p=1000.
p = (char*)((int*)(p));
At this statement character pointer p 1st typecasted to integer pointer and again typecasted to character pointer.
printf("%d, ", *p);-prints value 2.
As memory representation for little endian (lowest byte will stored 1st).
00000010 00000000 00000000 00000000
p = (int*)(p+1);-in this statement p which is char pointer is incremented by one so it is pointing at location 1001.
printf("%d", *p);-prints value 0 because at memory location 1001 value 0 is stored (according to little endian ).
Amit_Nawale said:
1 decade ago
Integer requires two bytes of storage
1 2 3 are stored in memory as
00000000 00000001 00000000 00000010 00000000 00000011 value
1000 1002 1004 address
p initially point to 1000,
As integer is two byte p+1 point to 1002,
Value at 1002= 00000000 00000010 i.e. 2
In previous example p is type cast to (Char *) in p = (char*)((int*)(p)); statement .
And in statement p = (int*)(p+1);
First p is incremented and that time type of p is char and char require 1 byte hence p point 1001 and then typecast to (int *)
1001 address does not exist hence ans in original question is 2,0
1 2 3 are stored in memory as
00000000 00000001 00000000 00000010 00000000 00000011 value
1000 1002 1004 address
p initially point to 1000,
As integer is two byte p+1 point to 1002,
Value at 1002= 00000000 00000010 i.e. 2
In previous example p is type cast to (Char *) in p = (char*)((int*)(p)); statement .
And in statement p = (int*)(p+1);
First p is incremented and that time type of p is char and char require 1 byte hence p point 1001 and then typecast to (int *)
1001 address does not exist hence ans in original question is 2,0
Alvar said:
1 decade ago
Assume the Base Address of arr as 1024.
p=arr => p=1024.
here The arr in which the elements are arranging in the order as below,
1024 => 2.
1028 => 3.
1032 => 4.
Then *p=>2, means *(1024) =>2.
while perform p=(int*)(p+1); here p as a char pointer p+1 gives the 1025 instead of 1028,because the p incremented by 1 byte rather than 4 byte where there as an integer pointer.
so afterwards *p points no where as *(1025),that's why the o/p gives the Zero by default for second print statement.
Thus the output become 2,0.
Hope all understand.
p=arr => p=1024.
here The arr in which the elements are arranging in the order as below,
1024 => 2.
1028 => 3.
1032 => 4.
Then *p=>2, means *(1024) =>2.
while perform p=(int*)(p+1); here p as a char pointer p+1 gives the 1025 instead of 1028,because the p incremented by 1 byte rather than 4 byte where there as an integer pointer.
so afterwards *p points no where as *(1025),that's why the o/p gives the Zero by default for second print statement.
Thus the output become 2,0.
Hope all understand.
Rajesh said:
1 decade ago
I didn't understand this. If we are typecasting to int and giving some address then it will print the contents up to next int size (2 bytes or 4 bytes) (depending on compiler) so here 2 is fine but next thing it should print 3. Given below is little Indian representation.
First byte is 2. Incrementing it goes to next byte. Then typecasting to int and printing should print next two bytes (sizeof int) i.e. 3.
00000010 00000000 00000011 00000000 00000100 00000000.
First byte is 2. Incrementing it goes to next byte. Then typecasting to int and printing should print next two bytes (sizeof int) i.e. 3.
00000010 00000000 00000011 00000000 00000100 00000000.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers