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

Nawaz khan said:   9 years ago
Excellent solution, Thanks @Viraj.

Nikhil said:   8 years ago
Guys run this code, you will get same output.

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

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

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

The compiler will produce an error.

Poornachandra h d said:   8 years ago
@Shivam Varshney : the answer is correct.

p=(char *)((int *)(p)); and p=(char *)(p) ; //both are same
so, let us consider starting address of p is 1000;
when we load p = (char *)p or p = (char *)((int *)(p));
the digit '2' is stored as character in 'p'. And next three bits are left as zero;

when you increment address by +1 execute, we get '0'.//now p is 1001
in the compiler, try by increasing 'p = p+4' and print --- you will get 3 //now p is 1004

(now you try printing p+1)//i.e, 1005 --- you will get zero
and again do 'p = p + 4' //now it is 1008.
what will it print now?

Yes, exactly next digit which is stored as char '4'.
Thank you!

Raj said:   8 years ago
If you take ptr as int rather char, then (p+1) gives you ans: let (p+0)=1000 then
(P+1)=1004.

In this example typecast is done to char as char is of 1Byte then by p+1 1byte address will get incremented but the arr is int 4 byte.

So, for char, next byte is 0 so (char*) p+1 is 0.

Amit said:   8 years ago
Here the answer will 2,0 because they are sting contains the following method of binary of 0001001, 0011100, 00010, 000001, 00010.

And also the memory allocation that add[200], add[201], and define the p+1 pointer *ptr (0+1) **ptr and they bytes store in memory and will be defined 2,0.

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)

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)

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


Post your comments here:

Your comments will be displayed after verification.