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 2 of 9.
Loki said:
10 years ago
The answer will give value based on whether it is little endian or big endian machine. Both p and p+1 points to address range or value 2. If any answer given the please explain or give note about differences on different machine.
Murthy said:
1 decade ago
"p=arr";
Cannot convert 'int*' to 'char*' in assignment.
Cannot convert 'int*' to 'char*' in assignment.
Danny said:
1 decade ago
Hey so I have a doubt like if you cast a pointer like this.
(int *)p+1;
That means you are adding 4 byte to that address and go to the next one is 3;
Can anybody explain this?
(int *)p+1;
That means you are adding 4 byte to that address and go to the next one is 3;
Can anybody explain this?
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.
Yogeshwar Singh said:
1 decade ago
Don't get confused by type conversions they are just used to confuse you. You can also write it like this:
#include<stdio.h>
int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = arr;
printf("%d, ", *p);
p = p+1;
printf("%d", *p);
return 0;
}
#include<stdio.h>
int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = arr;
printf("%d, ", *p);
p = p+1;
printf("%d", *p);
return 0;
}
Shalu said:
1 decade ago
o/p = ?
char *p;p = arr
(char*)(int*)p
printf("%d,p");
p = (int*)p;
printf("%d,p");
char *p;p = arr
(char*)(int*)p
printf("%d,p");
p = (int*)p;
printf("%d,p");
Iswarya said:
1 decade ago
(int*)p+1 comes to 2nd location in an array. Then how it prints zero?
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.
Komal said:
9 years ago
Very good and clear explanation. Thank you all.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers