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

Shreya said:   9 years ago
In step 2.

printf("%c", *(int*)vp);
Why doesn't it print 65 in place of 'A'?

(since it is typecast to int type and j also holds an integer).

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?

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?

Aswini said:   7 years ago
If it is like printf("%d",char(*)vp+2);then what would be the answer and how please explain?

Prashanth said:   10 years ago
Hi,

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

Sivaraj MCA said:   8 years ago
The void pointer can't accept any arithmetic operation then how it is possible?

Ponraj said:   9 years ago
@Nilesh.

I understand this with the help of your explanation. Thank you.

Saurav said:   1 decade ago
What is the difference between int and char typecasting. Please explain.

Shwetha said:   9 years ago
Thanks a lot, @Nilesh. Your method is simple, clear and seemed easy :).

Shubham said:   8 years ago
@Sriram.

Because there is %c format specifier in the printf function.


Post your comments here:

Your comments will be displayed after verification.