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

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

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

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

Jhunu said:   1 decade ago
Thanks nilesh.

Trupti said:   1 decade ago
Thanks nilesh.

Prashanth said:   1 decade ago
Hi,

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

Arup said:   1 decade ago
Explanation :
printf ("%c", * (int*) vp) ;
Here vp is a void pointer (void * vp)that can hold address of all type of variable.
Lets take an example
void *vp; // void pointer declaration
int a=5; // declaration of variable and assigned to 5
vp=&a;
Normally to show the value of the variable through pointer, we are just appending * before pointer i.e printf("%d",*p) that means it will show value at p . but here it will not show if we will declare like the above. Because vp is a void pointer that means it can hold all data type address .if we simply place *vp then compiler does not identify ..So for that we have to type cast ie (int *)vp ..it indicates vp hold the integer address only .if we wan to see that value just place * (int *)vp..
Then you will get the value of variable..but here %c format specifier used so it will convert that integer value to character value
A-Z(65-90)
a-z (92-122)
0-9 (48-57)

Thanks

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).

Ritesh said:   1 decade ago
Thanks nilesh bro.

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


Post your comments here:

Your comments will be displayed after verification.