Cast question

Discussion in 'C' started by bughunter2, May 25, 2007.

  1. bughunter2

    bughunter2 New Member

    Joined:
    Feb 27, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Netherlands
    Home Page:
    http://reversemasters.nl/
    For example I have this code:

    Code:
                /* increase gravity */
                if((1000/60) /* 60 Hz */ > m_iGravity)
                {
                    --m_iGravity;
                }
    
    Now 'm_iGravity' is declared as type 'int'. The compiler warning is: "comparison between signed and unsigned integer expressions".

    When I cast 'm_iGravity' as '(unsigned int)' I still get this warning, but if I modify the declaration of 'm_iGravity' to 'unsigned int' I get no warning. I also tried casting the result of the division result, but no luck.

    Can anyone give me a hand with this?
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You get the warning because the comparison could give you an error. For simplicity, consider that an int or unsigned int were one byte. 0xff would be -1 for the int, but 255 for the unsigned int. Casting won't fix the problem. It'll make the conversion, alright, but if you assign it to an int, that int will be interpretated, later, as an int. The cast converts the value, not the type of variable assigned to. All those could get you into an error situation. When you cast the result of the division, you should cast it to int, to match gravity, not unsigned int.
     
  3. bughunter2

    bughunter2 New Member

    Joined:
    Feb 27, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Netherlands
    Home Page:
    http://reversemasters.nl/
    Yes I understand it. However, I have already tried to cast the result of the division as 'int' and it didn't work. Could you show me the correct way of casting (maybe I'm doing it totally wrong?).
     
  4. bughunter2

    bughunter2 New Member

    Joined:
    Feb 27, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Netherlands
    Home Page:
    http://reversemasters.nl/
    I tried the following to cast the division result:

    Code:
                /* increase gravity */
                if((int)(1000/60) /* 60 Hz */ > m_iGravity)
                {
                    --m_iGravity;
                }
    
     
  5. bughunter2

    bughunter2 New Member

    Joined:
    Feb 27, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Netherlands
    Home Page:
    http://reversemasters.nl/
    Oh I'm sorry I was looking at the wrong line (been coding too long of course!).

    Actually the warning was about another line so that's why casting the result of the division didn't help anything.

    I'm very thankful though for your explanation, it helped me understand errors of the same type that I had for some of my other programs.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice