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

Mayur22kar said:   4 years ago
@All.

According to me; the coding part is;


#include<stdio.h>

int main()
{
// create and array hear with name arr

int arr[3] = {2, 3, 4};

// assume array is created at memory location 1000
// so 2 is store at location 1000 and
// 3 on 1004 and
// 4 is store at 1008......

// created one char pointer *p
char *p;
//p is pointing to the 1000
p = arr;

p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p+1); // when you increase hear p+1 then 1000 becomes 1001 because p is character pointer
printf("%d", *p);
return 0;
}
(11)

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)

TDas said:   5 years ago
Cannot convert an integer to a character pointer.

So,to get the desired output the revised code should be p=(char*)arr in place of p=arr and p=(char*)(int*)(p+1) in place of p=(int*)(p+1) .
(2)

Noel Nosse said:   7 years ago
Output is 0 except for { p = (int*)(p+0) }, { p = (int*)(p+4) } and { p = (int*)(p+8) }.
Per the previous question in is FOUR BYTES, so the binary saved in memory in NOT as above, but:
00000000 00000000 00000000 00000010 - first 4 bytes for 2
00000000 00000000 00000000 00000011 - second 4 bytes for 3
00000000 00000000 00000000 00000100 - third 4 bytes for 4.

The { p = (int*)(p+1); } is moving us ONLY 8 bites (one byte) at a time - AFTER STARTING with the "2" at the end of the first bytes. "p+2" carries us only 8 bits forward because of { char *p;...p = (char*)((int*)(p)); }.

Throughout p is a CHAR pointer moving only the 1 byte of a char.
(2)

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

Vaishu said:   7 years ago
The above code has displayed suspicious pointer conversion error @line p=(int*)(p+1); in turbo C.

then how it is compiled and displayed output?

Can anyone tell the memory address allocation for the above program?
(1)

Pankaj kumar said:   8 years ago
p = (char*)((int*)(p)); can anyone explain this?
(1)

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;
}

Pradeep said:   9 years ago
Nice explanation @Puru.

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.


Post your comments here:

Your comments will be displayed after verification.