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

Shrinivas Prabhu said:   1 decade ago
Thanks buddy, nice explanation.

Chinni said:   1 decade ago
Thanks a lot... dude.

Ganesh said:   1 decade ago
Thanks Nilesh nice explanation.

Preethi said:   1 decade ago
Thanks Nilesh.

Aditya said:   1 decade ago
Thanks Nilesh..

Manasa said:   1 decade ago
Why we have to write printf("%c", *(int*)vp); why not printf("%c",vp); if you don't mind, please explain?

Lokesh said:   1 decade ago
@Arun.

Great explanation. Finally I got it!

Ayush said:   1 decade ago
I couldn't understand how we get C after incrementing by +2 although cp points to memory address so how we get character C?

Uday said:   1 decade ago
#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;
}

A ASCII value is 65 and J ASCII value is 75.
and printf function in vp++2 so JACK first two letters are eliminated and ASCII values are replaced.

Xyz said:   1 decade ago
Thanks Nilesh. Well Explained.


Post your comments here:

Your comments will be displayed after verification.