Correct way to cast

Discussion in 'C Programming' started by Joe, Mar 14, 2007.

  1. Joe

    Joe Guest

    I need to cast the correct way

    HMENU mymenu;
    int stuff;

    ....
    stuff=(int)(long)(HMENU)mymenu;
    ....


    Currently it work, but my compiler is issueing warnings.
    In above example the mymenu -variable never exceed values 0 - 32000 allthough it
    is a HMENU type.

    Furthermore I am getting warnings from comparisons:

    ....
    if ( (HMENU)(int)stuff==(int)(long)(HMENU)mymenu )
    ....

    Again it works but is non-standard casting. I'd like to get rid of warning
    though.
    Joe, Mar 14, 2007
    #1
    1. Advertising

  2. Joe

    Ian Collins Guest

    Joe wrote:
    > I need to cast the correct way
    >
    > HMENU mymenu;


    What's an HMENU?

    > int stuff;
    >
    > ....
    > stuff=(int)(long)(HMENU)mymenu;


    Why so many casts?

    > ....
    >
    >
    > Currently it work, but my compiler is issueing warnings.
    > In above example the mymenu -variable never exceed values 0 - 32000 allthough it
    > is a HMENU type.
    >
    > Furthermore I am getting warnings from comparisons:
    >
    > ....
    > if ( (HMENU)(int)stuff==(int)(long)(HMENU)mymenu )
    > ....
    >

    I'm not surprised!

    --
    Ian Collins.
    Ian Collins, Mar 14, 2007
    #2
    1. Advertising

  3. "Joe" <> wrote in message
    >
    >I need to cast the correct way
    >
    > HMENU mymenu;
    > int stuff;
    >
    > ...
    > stuff=(int)(long)(HMENU)mymenu;
    > ...
    >
    >
    > Currently it work, but my compiler is issueing warnings.
    > In above example the mymenu -variable never exceed values 0 - 32000
    > allthough it
    > is a HMENU type.
    >
    > Furthermore I am getting warnings from comparisons:
    >
    > ...
    > if ( (HMENU)(int)stuff==(int)(long)(HMENU)mymenu )
    > ...
    >
    > Again it works but is non-standard casting. I'd like to get rid of warning
    > though.
    >

    You are not meant to know what an HMENU is. That's why is an idenfier rather
    than a basic type.
    You documentation may specify integer or other values to be cast to HMENUs,
    particuarly 0 or NULL. However normally you don't undertake the reverse
    process. Let the system manipulate HMENUs internally to its heart's content,
    but don't try to look inside them yourself.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Mar 14, 2007
    #3
  4. Joe <> writes:
    > I need to cast the correct way


    No, you probably don't.

    > HMENU mymenu;
    > int stuff;
    >
    > ...
    > stuff=(int)(long)(HMENU)mymenu;
    > ...
    >
    >
    > Currently it work, but my compiler is issueing warnings. In above
    > example the mymenu -variable never exceed values 0 - 32000 allthough
    > it is a HMENU type.
    >
    > Furthermore I am getting warnings from comparisons:
    >
    > ...
    > if ( (HMENU)(int)stuff==(int)(long)(HMENU)mymenu )
    > ...
    >
    > Again it works but is non-standard casting. I'd like to get rid of warning
    > though.


    Close your eyes; you won't see the warning. :cool:}

    What warning is your compiler giving you? Don't summarize or
    paraphrase it; copy-and-paste the *exact* warning message.

    *Most* casts are unnecessary. Programmers (too) often add casts to
    silence compiler warnings. This is like putting a piece of tape over
    the red warning light on your car's dashboard; the warning is gone,
    but the problem is still there.

    I have no idea what an HMENU is. If it's an arithmetic type, you can
    do this:

    HMENU mymenu;
    int stuff;
    ... /* presumably mymenu is assigned a value */
    stuff = mymenu;

    Looking at your assignment:

    stuff=(int)(long)(HMENU)mymenu;

    mymenu is already of type HMENU, so casting it (i.e., explicitly
    converting it) to HMENU is useless. Converting from HMENU to long
    might be a sensible thing to do, but converting from HMENU to long to
    int doesn't make much sense; you might as well convert directly from
    HMENU to int. Finally, stuff is of type int, so if HMENU is an
    arithmetic type, it will be converted implicitly; the cast is not
    necessary.

    If HMENU is *not* of an arithmetic type, then it may be of a pointer
    type. Converting a pointer to an integer is allowed, and generally
    requires an explicit cast operator, but it's rarely a good idea.
    *If* that's what you really want to do, then the way to do it is:

    stuff = (int)mymenu;

    but the result is implementation-specific, and likely to be
    meaningless.

    However HMENU is defined, you should consider carefully whether you
    want to assign an HMENU value to an int object. I suspect that HMENU
    is intended to be an abstract type, with whatever software package
    provides the time providing all the operations you need to perform on
    it. What are you planning to do with "stuff" after you assign the
    value of mymenu to it?

    I think you're probably committing a classic new programmer's mistake.
    You're trying to pound a nail with a screwdriver, and the handles keep
    breaking, so you ask how to strengthen a screwdriver's handle. We can
    suggest various ways to do that, but we can be a *lot* more helpful if
    you ask us how to pound a nail.

    A bit of Googling indicates that HMENU is probably a Windows-specific
    typedef. If you want to know how to work with HMENUs, consult your
    documentation or post to a Windows-specific newsgroup.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 14, 2007
    #4
  5. Joe

    CBFalconer Guest

    Joe wrote:
    >
    > I need to cast the correct way
    >
    > HMENU mymenu;
    > int stuff;
    >
    > ...
    > stuff=(int)(long)(HMENU)mymenu;
    > ...
    >
    > Currently it work, but my compiler is issueing warnings.
    > In above example the mymenu -variable never exceed values 0 - 32000
    > allthough it is a HMENU type.
    >
    > Furthermore I am getting warnings from comparisons:
    >
    > ...
    > if ( (HMENU)(int)stuff==(int)(long)(HMENU)mymenu )
    > ...
    >
    > Again it works but is non-standard casting. I'd like to get rid of
    > warning though.


    How can you possibly expect an answer without showing the
    definition of HMENU?

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 14, 2007
    #5
  6. Joe

    CBFalconer Guest

    Malcolm McLean wrote:
    >

    .... snip ...
    >
    > You are not meant to know what an HMENU is. That's why is an
    > idenfier rather than a basic type.


    "grep HMENU n869.txt" turns up no hits. The thing doesn't exist.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 15, 2007
    #6
  7. On Mar 14, 10:45 pm, Keith Thompson <> wrote:
    > mymenu is already of type HMENU, so casting it (i.e., explicitly
    > converting it) to HMENU is useless. Converting from HMENU to long
    > might be a sensible thing to do, but converting from HMENU to long to
    > int doesn't make much sense; you might as well convert directly from
    > HMENU to int.


    Most likely this is done to avoid a warning. If HMENU is some kind of
    pointer, and lets say pointers and longs are eight bytes, ints are
    four bytes, then it is likely that casting HMENU to int will give a
    warning (casting a pointer to an integer with fewer bytes is quite
    likely to be wrong), while casting from HMENU to long is fine, and
    casting from long to int is fine as well. Not that it is any different
    than casting to int directly, but in this case the cast to an int with
    fewer bits is clearly intentional and what the programmer wanted.
    christian.bau, Mar 15, 2007
    #7
  8. Joe <> wrote:
    > I need to cast the correct way


    > HMENU mymenu;
    > int stuff;


    > ...
    > stuff=(int)(long)(HMENU)mymenu;
    > ...


    The HMENU is a platform-specific extension (probably a typedef),
    and I really doubt that a cast to int is legal on said platform.

    > ...
    > if ( (HMENU)(int)stuff==(int)(long)(HMENU)mymenu )
    > ...


    Or such a comparison. You have to use the API they give you,
    or all bets are off.


    --
    pa at panix dot com
    Pierre Asselin, Mar 15, 2007
    #8
  9. In article <>,
    Joe <> wrote:
    >I need to cast the correct way


    The correct way is usually "Don't". Most conversions that don't happen
    without a cast are unsafe, and you want to know what you're doing before
    you use them.


    >HMENU mymenu;


    (HMENU is a type defined by the API for a fairly common semi-hosted
    C implementation; it should usually be treated as a magic cookie by
    programs, but underneath it's a type that can be converted to and
    from longer-than-short integer types and pointer types without losing
    information.)

    >int stuff;
    >
    >...
    >stuff=(int)(long)(HMENU)mymenu;


    At least two of these casts are useless. If you really want to convert
    the value of stuff into an int, you can just say:

    stuff=(int)mymenu;


    >Currently it work, but my compiler is issueing warnings.


    Unless the warning is something along the lines of "I don't think you
    know what you're Really Trying To Do", I suspect that it's warning about
    something other than the line of code you've shown us.


    >Furthermore I am getting warnings from comparisons:
    >
    >...
    >if ( (HMENU)(int)stuff==(int)(long)(HMENU)mymenu )
    >...


    Not only do you have no fewer than four useless casts here, but you're
    also trying to compare a value of type HMENU with a value of type int.
    Decide whether you want to compare the values as ints or as HMENUs,
    and cast the one that's not that type to that type.


    >Again it works but is non-standard casting. I'd like to get rid of warning
    >though.


    Stop cargo-culting and figure out what it is you're trying to do, then
    do that.
    If you still have problems, they're probably going to be
    specific to whatever it is you're trying to do with the HMENU, so
    comp.os.ms-windows.programmer.win32 would probably be a better place to
    ask than here.


    dave

    --
    Dave Vandervies
    t is only logical to deduct that you don't exist either. Since you
    don't exist, nobody voted for the standard you described, thus there
    is no standard. --Daniel Fischer in comp.lang.c
    Dave Vandervies, Mar 15, 2007
    #9
  10. Pierre Asselin said:

    > Joe <> wrote:
    >> I need to cast the correct way

    >
    >> HMENU mymenu;
    >> int stuff;

    >
    >> ...
    >> stuff=(int)(long)(HMENU)mymenu;
    >> ...

    >
    > The HMENU is a platform-specific extension (probably a typedef),
    > and I really doubt that a cast to int is legal on said platform.


    It's almost certainly a pointer, and casting a pointer into an int is
    always legal (although the result isn't necessarily what you'd like it
    to be). If it's a Win32 program, it /is/ a pointer, and the conversion
    to a 32-bit int for comparison purposes, whilst unlikely to be the best
    way to do things, is unlikely to cause too many problems.

    But, thinking clearly about this for a moment, we can surely see that
    there is no *point* in comparing an HMENU against anything other than
    another HMENU, and for this purpose no cast is required.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Mar 15, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joon
    Replies:
    1
    Views:
    515
    Roedy Green
    Jul 8, 2003
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,072
    Dan Pop
    Feb 10, 2004
  3. EvilRix
    Replies:
    8
    Views:
    633
    Martin Dickopp
    Feb 14, 2004
  4. Pavel
    Replies:
    7
    Views:
    524
    Pavel
    Sep 19, 2010
  5. The correct cast

    , Jan 13, 2011, in forum: C++
    Replies:
    7
    Views:
    274
    Paul Brettschneider
    Jan 13, 2011
Loading...

Share This Page