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

Prashanth said:   10 years ago
Hi,

Can anybody say, how you are telling ASCII values like 74=j and 65=a old tel me.

Saurabh said:   10 years ago
How to convert those numbers in ASCII values?

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.

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

Chintan said:   1 decade ago
Thank you Nilesh :).

Ejaz said:   1 decade ago
Thanks nilesh. :).


Post your comments here:

Your comments will be displayed after verification.