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 7 of 9.
Vartika said:
10 years ago
p = (int*)(p+1);
printf("%d",*p);
This should give the output as 3 because %d access 2 bytes of memory so even if we start accessing the memory from 2nd byte from the left, answer should be 3 i.e. 00000000 00000011.
printf("%d",*p);
This should give the output as 3 because %d access 2 bytes of memory so even if we start accessing the memory from 2nd byte from the left, answer should be 3 i.e. 00000000 00000011.
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)
Pradeep said:
9 years ago
Nice explanation @Puru.
Ishu said:
9 years ago
It will result in error, can't convert (int*) to (char*).
Sayli said:
9 years ago
What is meant by an endian method?
Bunny said:
9 years ago
Those who are getting an error.
In some compiler, you can equate two different data type pointers so you have to typecast them.
Let's say the base address of the array is 200.
p = (char*)(arr+1);
Here p will be 204.
If, p = (char*) arr+1;
Then it will be 201.
In some compiler, you can equate two different data type pointers so you have to typecast them.
Let's say the base address of the array is 200.
p = (char*)(arr+1);
Here p will be 204.
If, p = (char*) arr+1;
Then it will be 201.
Komal said:
9 years ago
Very good and clear explanation. Thank you all.
DISH said:
9 years ago
Do we need to typecast the pointer every time?
Muhammad Imran said:
9 years ago
p = (char*)((int*)(p));
Why do this type casting?
Why do this type casting?
Muhammad Imran said:
9 years ago
Type casting means?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers