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 2 of 9.
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();
}
Lucky said:
1 decade ago
ch is character variable then hw would we assign it to 74?
Cheran said:
1 decade ago
Thanks Nilesh....Good Job
Waste body said:
1 decade ago
Thanks nilesh.
Sujith said:
1 decade ago
Thank you very much NILESH.
Divya said:
1 decade ago
Thank you very much Nilesh.
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.
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..
Zohra said:
1 decade ago
Thanks Nilesh. Well explained.
Kavya said:
1 decade ago
Thanks Nilesh, nice explanation.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers