C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 10)
10.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    void *vp;
    char ch=74, *cp="JACK";
    int j=65;
    vp=&ch;
    printf("%c", *(char*)vp);
    vp=&j;
    printf("%c", *(int*)vp);
    vp=cp;
    printf("%s", (char*)vp+2);
    return 0;
}
JCK
J65K
JAK
JACK
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
84 comments Page 3 of 9.

Chitra said:   1 decade ago
ch=74 which is the ASCII value of J
j=65 which is the ASCII value of A
vp a void pointer contains the address of ch so it can be type casted to any type of pointer.
printf("%c", *(char*)vp);
(char*)vp type castes void type vp to char type and prints the value pointed by vp which is J.
vp=&j;
printf("%c", *(int*)vp);
Now vp contains the address of j.
*(int*)vp it type castes void type pointer vp to int type and prints the value pointed by vp which is A
vp=cp;
printf("%s", (char*)vp+2);
Now vp contains the value of cp. pointer vp is incremented by 2.so now it points to the letter C of JACK and prints the string CK.

So finally JACK is printed.

Savithri.s.p said:   1 decade ago
Hey really very accurate answer.

Umamahesh said:   1 decade ago
Really fantastic explanation by nilesh good job.

Roshan said:   1 decade ago
Thanks nilesh.

Satya said:   1 decade ago
Hello friends!

Will anyone explain the meaning of

printf ("%c", * (int*) vp) ;

As I am new in this field, will anyone took mercy on me and explain it ?

Kusuma said:   1 decade ago
Really fantastic explanation by nilesh

Gaurav said:   1 decade ago
Thanks nilesh.

Yosuva A said:   1 decade ago
Thanks Nilesh. Well explained.

Ritesh said:   1 decade ago
Thanks nilesh bro.

Ritesh said:   1 decade ago
Hi satya.

This mens, the pronunciation of pointer alwys strt from right to left, so firstly we typecast the vp because vp is othr datatype of pointer nd we want to intgr pointer so we use typecasting nd aftr tht we solve *vp (mens value at vp or value at adress stored in vp).


Post your comments here:

Your comments will be displayed after verification.