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;
}
2, 3
2, 0
2, Garbage value
0, 0
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
87 comments Page 4 of 9.

Rupinderjit said:   1 decade ago
Viraj is right.

If you want to get 3 as output, use (p+2) and proper typecasting.

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).

Prakash N D said:   1 decade ago
Its simple whenever you increment the pointer it will incremented by 4. Because all pointer of size is 4 bytes. Since array contained 4 elements but, last one not initialized the result is zero.

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).

Siva said:   1 decade ago
#include<iostream>
int main()
{
using namespace std;
int a[]={1,2,3};
int *p;
p=a;
p=p+1;
cout<<*p;
return 0;
}

This will return 2, why? It is similar to that of the above question.

Amit_Nawale said:   1 decade ago
Integer requires two bytes of storage
1 2 3 are stored in memory as
00000000 00000001 00000000 00000010 00000000 00000011 value

1000 1002 1004 address

p initially point to 1000,
As integer is two byte p+1 point to 1002,

Value at 1002= 00000000 00000010 i.e. 2
In previous example p is type cast to (Char *) in p = (char*)((int*)(p)); statement .


And in statement p = (int*)(p+1);

First p is incremented and that time type of p is char and char require 1 byte hence p point 1001 and then typecast to (int *)

1001 address does not exist hence ans in original question is 2,0

Jitendra said:   1 decade ago
@viraj you are excellent . this type of explanations make concept clear.

Jhunu said:   1 decade ago
Thanks amit. Its cleared my doubt.

Rookie said:   1 decade ago
It causes a compile time error as it is an incompatible type conversion p being an character pointer is referring to int base array.

Anyone please explain the correct logic.

Sarala said:   1 decade ago
It causes a compile time error as it is an incompatible type conversion p being an character pointer is referring to int base array.

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.


Post your comments here:

Your comments will be displayed after verification.