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

DISH said:   9 years ago
Do we need to typecast the pointer every time?

Manishj said:   1 decade ago
It gives warning as incompatible pointer type.

Answer 2, 0 is correct.

It doesn't show errors.

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

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.

Rahul p said:   1 decade ago
@viraj

Your answer is correct. But here integer requires 4 bytes of storage

Hence, when

p = (int*)(p+4); It will print next element of arr which is 3.

&

p = (int*)(p+8);It will print next element of arr which is 4.

Vijay Singh said:   1 decade ago
It's give error in turbo c3
p = arr; \\ can not convert "int *" to "char *"
p = (int*)(p+1); \\ can not convert "int *" to "char *"

Madhuri said:   1 decade ago
Good explanation viraj.

Shashank said:   1 decade ago
Please tell me how this program will execute.

Himanshu batra said:   1 decade ago
Happy Budday viraj. Great job.

Nahak said:   1 decade ago
Nice one viraj. Really helpful.


Post your comments here:

Your comments will be displayed after verification.