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:
85 comments Page 7 of 9.

Shrinivas Prabhu said:   2 decades ago
Thanks buddy, nice explanation.

Chinni said:   2 decades ago
Thanks a lot... dude.

Ganesh said:   2 decades ago
Thanks Nilesh nice explanation.

Preethi said:   2 decades ago
Thanks Nilesh.

Aditya said:   1 decade ago
Thanks Nilesh..

Parveen said:   1 decade ago
@Manasa.

Because vp is void pointer. When we use void pointer for any data type then we have to typecast that void pointer with the datatype of the variable whose address is stored into that void pointer.

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.


Post your comments here:

Your comments will be displayed after verification.