Reversing (char)...

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 ...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up Reversing (char)...

    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

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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

  3. #3
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Thanks, Richie!

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Okay, so I understand it now but how do you do string to char and vise versa?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by yaya View Post
    Okay, so I understand it now but how do you do string to char and vise versa?
    A (C-style) string is a sequence of characters terminated by and including the first null character.

    Given that, what are you asking?
    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.*

  7. #7
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    It doesn't seem to work with strings.
    For example:

    Code:
    float a=1.1;
    cout<<(int)a;
    This displays '1' as it should but when I do this:

    Code:
    string a=("a");
    cout<<(char)a;
    it comes up with errors. Am I doing something wrong?

  8. #8
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    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];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 08:36 AM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 10:22 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21