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.

Ankita said:   1 decade ago
What is this endian method?

Raghav said:   1 decade ago
(char*)((int*)(p))
Could someone explain this?

Garvita said:   1 decade ago
Why we type casted p again to char?

p=(char*)((int*)(p));

Sanjoy said:   1 decade ago
What is type casted?

Vaibhav said:   1 decade ago
p = (int*)(p+1) ; what it does actually ?

Shivam varshney said:   8 years ago
The answer is wrong here.

The compiler will produce an error.

Abdul said:   4 years ago
Well done @Mayur.

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

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.

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.


Post your comments here:

Your comments will be displayed after verification.