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 3 of 9.

Komal said:   9 years ago
Very good and clear explanation. Thank you all.

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.

Sayli said:   9 years ago
What is meant by an endian method?

Ishu said:   9 years ago
It will result in error, can't convert (int*) to (char*).

Pradeep said:   9 years ago
Nice explanation @Puru.

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

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.

Loki said:   10 years ago
The answer will give value based on whether it is little endian or big endian machine. Both p and p+1 points to address range or value 2. If any answer given the please explain or give note about differences on different machine.

Murthy said:   1 decade ago
"p=arr";

Cannot convert 'int*' to 'char*' in assignment.

Danny said:   1 decade ago
Hey so I have a doubt like if you cast a pointer like this.

(int *)p+1;

That means you are adding 4 byte to that address and go to the next one is 3;

Can anybody explain this?


Post your comments here:

Your comments will be displayed after verification.