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 4 of 9.
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.
Tathagata said:
1 decade ago
The given problem should be like this, otherwise it may generate compile time error.
#include<stdio.h>
int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = (char*)arr;
p = (char*)((int*)(p));
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 = (char*)arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (p+1);
printf("%d", *p);
return 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.
Ankita said:
1 decade ago
What is this endian method?
Raghav said:
1 decade ago
(char*)((int*)(p))
Could someone explain this?
Could someone explain this?
Garvita said:
1 decade ago
Why we type casted p again to char?
p=(char*)((int*)(p));
p=(char*)((int*)(p));
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers