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 1 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
Nilesh said:
1 decade ago
Pointer always store integer value so cp will store the memory address of location where string "jack " is stored.
Step 1 : vp = &ch;
/*Will store address of ch in vp so while we print content in printf it will print asccii value of 74 i.e "J"*/
Step 2 : vp = &j;
/* It will assign address of j to vp again it will print ascii value of 65 as "A"*/
Step 3 : vp = cp;
/* In this step cp is pointing to memory locatioon where string jack is stored and we r incrementing it by two so it will point to "C" from sring "JACK" and since we hava given %S in printf so it will print content from c onwords ie "CK"*/
So final combined output will be JACK.
Step 1 : vp = &ch;
/*Will store address of ch in vp so while we print content in printf it will print asccii value of 74 i.e "J"*/
Step 2 : vp = &j;
/* It will assign address of j to vp again it will print ascii value of 65 as "A"*/
Step 3 : vp = cp;
/* In this step cp is pointing to memory locatioon where string jack is stored and we r incrementing it by two so it will point to "C" from sring "JACK" and since we hava given %S in printf so it will print content from c onwords ie "CK"*/
So final combined output will be JACK.
(47)
Bijan said:
1 decade ago
Hi sourav,
Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable. (char)a will make 'a' function as a char.
#include <iostream>
using namespace std;
int main()
{
cout<< (char)65 <<"\n";
// The (char) is a typecast, telling the computer to interpret the 65 as a
// character, not as a number. It is going to give the character output of
// the equivalent of the number 65 (It should be the letter A for ASCII).
cin.get();
}
Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable. (char)a will make 'a' function as a char.
#include <iostream>
using namespace std;
int main()
{
cout<< (char)65 <<"\n";
// The (char) is a typecast, telling the computer to interpret the 65 as a
// character, not as a number. It is going to give the character output of
// the equivalent of the number 65 (It should be the letter A for ASCII).
cin.get();
}
Chitra said:
1 decade ago
ch=74 which is the ASCII value of J
j=65 which is the ASCII value of A
vp a void pointer contains the address of ch so it can be type casted to any type of pointer.
printf("%c", *(char*)vp);
(char*)vp type castes void type vp to char type and prints the value pointed by vp which is J.
vp=&j;
printf("%c", *(int*)vp);
Now vp contains the address of j.
*(int*)vp it type castes void type pointer vp to int type and prints the value pointed by vp which is A
vp=cp;
printf("%s", (char*)vp+2);
Now vp contains the value of cp. pointer vp is incremented by 2.so now it points to the letter C of JACK and prints the string CK.
So finally JACK is printed.
j=65 which is the ASCII value of A
vp a void pointer contains the address of ch so it can be type casted to any type of pointer.
printf("%c", *(char*)vp);
(char*)vp type castes void type vp to char type and prints the value pointed by vp which is J.
vp=&j;
printf("%c", *(int*)vp);
Now vp contains the address of j.
*(int*)vp it type castes void type pointer vp to int type and prints the value pointed by vp which is A
vp=cp;
printf("%s", (char*)vp+2);
Now vp contains the value of cp. pointer vp is incremented by 2.so now it points to the letter C of JACK and prints the string CK.
So finally JACK is printed.
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.
Pragnya said:
1 decade ago
For @Lucky:
ch is a character variable. From the character set, we can assign any kind of values to the character variable. The character set consists of floating point no. s, integers, characters, special characters etc. which when given charater datatype behave as character.
ch is a character variable. From the character set, we can assign any kind of values to the character variable. The character set consists of floating point no. s, integers, characters, special characters etc. which when given charater datatype behave as character.
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).
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.
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.
Divya said:
1 decade ago
q = (int**)&p;
Here void ptr is casted into integer ptr.
this was d explantion given for typecasting in 1 of the discussion...
*(char*)vp-dis is also typecasting.. but both differs..
Here void ptr is casted into integer ptr.
this was d explantion given for typecasting in 1 of the discussion...
*(char*)vp-dis is also typecasting.. but both differs..
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