I'm not sure if this is possible, but is it possible to reverse the "(char)" function?
For example:
Code:(char)97 == a //but I want to do a == 97
This is a discussion on Reversing (char)... within the C++ Programming forums, part of the General Programming Boards category; I'm not sure if this is possible, but is it possible to reverse the "(char)" function? For example: Code: (char)97 ...
I'm not sure if this is possible, but is it possible to reverse the "(char)" function?
For example:
Code:(char)97 == a //but I want to do a == 97
First off, (char) is what is called a C-style cast, it's not called a function, but that's not too important in answering the question i suppose...
What's more important is that you realise what casting is, and how it is performed.
Short answer is that yes you can... depending on the types. If 'a' is a char, you can compare it to an integer - in that particular case you don't even need the (char) in font of it, the compiler understands what you're doing - this is called an implicit cast and the other one is called an explicit cast.
Some short examples of C-style casts
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI
just in the first sample 97 will be cast to char and compared to a
in the second - a will be cast to int and compared to 97
In most cases it will show no difference, but in some - it can... For example comparing to 255 when the char is signed will bring different results...
If I have eight hours for cutting wood, I spend six sharpening my axe.
Okay, so I understand it now but how do you do string to char and vise versa?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
It doesn't seem to work with strings.
For example:
This displays '1' as it should but when I do this:Code:float a=1.1; cout<<(int)a;
it comes up with errors. Am I doing something wrong?Code:string a=("a"); cout<<(char)a;
Of cource you are. A string object is an object with many member variables and a string representation consisting of two characters, a and \0. How would you cast this whole thing to a 1-byte variable?
I guess what you want to do is
Code:string a = "a"; cout << a[0];