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 8 of 9.
Ankur said:
1 decade ago
@pankaj.
Dude run it on turbo c it won't give you an error.
Dude run it on turbo c it won't give you an error.
Abhi said:
1 decade ago
@ pankaj
p is char type pointer and v cant assign int type arr to it . so ... it says suspicious ptr and in the next line if v dint do any type casting thn also it ill execute . and the line p=(char*)((int*)(p))); in the sense v r type castng the p to int and thn whole to char type.
since char has 1 byte it stores oly 1st byte tht is 2 and wen v increment it gives value 0
p is char type pointer and v cant assign int type arr to it . so ... it says suspicious ptr and in the next line if v dint do any type casting thn also it ill execute . and the line p=(char*)((int*)(p))); in the sense v r type castng the p to int and thn whole to char type.
since char has 1 byte it stores oly 1st byte tht is 2 and wen v increment it gives value 0
Lucky said:
1 decade ago
p = (char*)((int*)(p)); means?
Pankaj said:
1 decade ago
I tried to run this program but it gives compiletime error:
"suspicious pointer conversion" for below lines
p = arr;
p = (int*)(p+1);
"suspicious pointer conversion" for below lines
p = arr;
p = (int*)(p+1);
Rahul p said:
2 decades ago
@viraj
Your answer is correct. But here integer requires 4 bytes of storage
Hence, when
p = (int*)(p+4); It will print next element of arr which is 3.
&
p = (int*)(p+8);It will print next element of arr which is 4.
Your answer is correct. But here integer requires 4 bytes of storage
Hence, when
p = (int*)(p+4); It will print next element of arr which is 3.
&
p = (int*)(p+8);It will print next element of arr which is 4.
Adi said:
2 decades ago
Well done viraj.
Dilbagh said:
2 decades ago
Viraj you are excellent.
Nitin said:
2 decades ago
Well explained Viraj.
Viraj said:
2 decades ago
The answer is correct. Assuming that integer requires 2 bytes of storage
the integer array will be stored in memory as
00000010 00000000 00000011 00000000 00000100 00000000
This is because little-endian method is used to store bytes in memory i.e. least significant bytes are stored first.
When a character pointer is incremented by it points to the next byte. Hence the answer.
the integer array will be stored in memory as
00000010 00000000 00000011 00000000 00000100 00000000
This is because little-endian method is used to store bytes in memory i.e. least significant bytes are stored first.
When a character pointer is incremented by it points to the next byte. Hence the answer.
(1)
Purushotham said:
2 decades ago
p = arr;
causes compile time error
causes compile time error
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers