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 4 of 9.
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
Aditya said:
1 decade ago
Thanks Nilesh..
Trupti said:
1 decade ago
Thanks nilesh.
Jhunu said:
1 decade ago
Thanks nilesh.
Ejaz said:
1 decade ago
Thanks nilesh. :).
Chintan said:
1 decade ago
Thank you Nilesh :).
Xyz said:
1 decade ago
Thanks Nilesh. Well Explained.
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.
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.
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?
Lokesh said:
1 decade ago
@Arun.
Great explanation. Finally I got it!
Great explanation. Finally I got it!
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers