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.
Praveen kumar said:
1 decade ago
since integer requires 4 bytes=32 bits so 2,3,4 stored as
==>{lsb first then msb}
00000000 00000000 00000000 00000010 00000000 00000000 00000000
103 102 101 100 107 106 105
00000011 00000000 00000000 00000000 00000100
104 111 110 109 108
Initially,p holds address=100
So value at addr 100=2.
Now,it casted to char ptr,
So int*(p+1) =value at addr 100+1=0.
==>{lsb first then msb}
00000000 00000000 00000000 00000010 00000000 00000000 00000000
103 102 101 100 107 106 105
00000011 00000000 00000000 00000000 00000100
104 111 110 109 108
Initially,p holds address=100
So value at addr 100=2.
Now,it casted to char ptr,
So int*(p+1) =value at addr 100+1=0.
Viraj said:
1 decade 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)
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
Nirav said:
1 decade ago
Here p = arr= points to the first element of the array
p = (char*)((int*)(p))=type cast value 2 to char..
'2' = ('2') (2)
But
printf("%d, ", *p) = print the integer value to 2
Because: p+1 points to upper byte of 3 as per little-endian method, which is 0 (00000000 00000011) and p+2 points to 3, lower bye of three (00000000 00000011).
p = (char*)((int*)(p))=type cast value 2 to char..
'2' = ('2') (2)
But
printf("%d, ", *p) = print the integer value to 2
Because: p+1 points to upper byte of 3 as per little-endian method, which is 0 (00000000 00000011) and p+2 points to 3, lower bye of three (00000000 00000011).
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;
}
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;
}
Nikhil said:
8 years ago
Guys run this code, you will get same output.
#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;
}
Amit said:
8 years ago
Here the answer will 2,0 because they are sting contains the following method of binary of 0001001, 0011100, 00010, 000001, 00010.
And also the memory allocation that add[200], add[201], and define the p+1 pointer *ptr (0+1) **ptr and they bytes store in memory and will be defined 2,0.
And also the memory allocation that add[200], add[201], and define the p+1 pointer *ptr (0+1) **ptr and they bytes store in memory and will be defined 2,0.
Raj said:
8 years ago
If you take ptr as int rather char, then (p+1) gives you ans: let (p+0)=1000 then
(P+1)=1004.
In this example typecast is done to char as char is of 1Byte then by p+1 1byte address will get incremented but the arr is int 4 byte.
So, for char, next byte is 0 so (char*) p+1 is 0.
(P+1)=1004.
In this example typecast is done to char as char is of 1Byte then by p+1 1byte address will get incremented but the arr is int 4 byte.
So, for char, next byte is 0 so (char*) p+1 is 0.
SHAHIDNX said:
1 decade ago
First it will perform outer typecast then it will perform inner typecast.
And in second printf since pointer type is char so it point to 1byte data. Hence it will fetch the first byte of value 2 and p+1 point to second byte of value 2.
So it is zero (2=0000000000000010).
And in second printf since pointer type is char so it point to 1byte data. Hence it will fetch the first byte of value 2 and p+1 point to second byte of value 2.
So it is zero (2=0000000000000010).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers