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.

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.

Shalu said:   1 decade ago
o/p = ?

char *p;p = arr
(char*)(int*)p
printf("%d,p");
p = (int*)p;
printf("%d,p");

Aakash Gupta said:   1 decade ago
In borland and turbo c++ it gives the error: Cannot Convert "int*" to "char*".

Swati said:   1 decade ago
It gives error "cannot convert 'int*' to 'char*'" in both lines

p=arr;
p=(int*)(p+1);

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

Answer 2, 0 is correct.

It doesn't show errors.

Varni said:   1 decade ago
Why not upper byte of 2 not considered here ? (00000000).

Is it put at the end of the array ?

Ravi said:   2 decades ago
p=arr;//here p is a character pointer. we should not assign int array base address...

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

Jitendra said:   1 decade ago
@viraj you are excellent . this type of explanations make concept clear.

Vivek said:   1 decade ago
char p stored in int type arr. which is not possible without conversion.


Post your comments here:

Your comments will be displayed after verification.