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.

Abdul said:   3 years ago
Well done @Mayur.

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;
}
(10)

Akash said:   5 years ago
All droughts are cleared, Thanks for explaining @Puru.

Shivani Mundra said:   5 years ago
It will give a warning instead of error hence p=arr is illegal but this code will compile and give output.

But my question is how to know that these integers will be stored in little-endian firm or big-endian?

Please, anyone, help me.

Pushparaj said:   5 years ago
Just, (p+1) is NULL. (p+2) is NULL. (p+2) is NULL. (p+4) is NULL. etc
Otherwise, p=(int*)(p+1) is NULL or ZERO.

TDas said:   5 years ago
Simply p=p+1 works. No need to convert integer pointer then character pointer.

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)

Sachin said:   6 years ago
Can anyone explain this to get it?

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)

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)


Post your comments here:

Your comments will be displayed after verification.