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;
}
Discussion:
84 comments Page 6 of 9.
Jhunu said:
1 decade ago
Thanks nilesh.
Trupti said:
1 decade ago
Thanks nilesh.
Aditya said:
1 decade ago
Thanks Nilesh..
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
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).
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.
Gaurav said:
1 decade ago
Thanks nilesh.
Kusuma said:
1 decade ago
Really fantastic explanation by nilesh
Satya said:
1 decade ago
Hello friends!
Will anyone explain the meaning of
printf ("%c", * (int*) vp) ;
As I am new in this field, will anyone took mercy on me and explain it ?
Will anyone explain the meaning of
printf ("%c", * (int*) vp) ;
As I am new in this field, will anyone took mercy on me and explain it ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers