Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Correct way to cast

Reply
Thread Tools

Correct way to cast

 
 
Joe
Guest
Posts: n/a
 
      03-14-2007
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.


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-14-2007
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.
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      03-14-2007
"Joe" <(E-Mail Removed)> 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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-14-2007
Joe <(E-Mail Removed)> 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. }

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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-14-2007
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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-15-2007
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

 
Reply With Quote
 
christian.bau
Guest
Posts: n/a
 
      03-15-2007
On Mar 14, 10:45 pm, Keith Thompson <(E-Mail Removed)> 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.

 
Reply With Quote
 
Pierre Asselin
Guest
Posts: n/a
 
      03-15-2007
Joe <(E-Mail Removed)> 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
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      03-15-2007
In article <(E-Mail Removed)>,
Joe <(E-Mail Removed)> 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 (E-Mail Removed)
[I]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
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-15-2007
Pierre Asselin said:

> Joe <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
`How to easily correct colour cast in photos Peter Stacey Digital Photography 6 04-09-2007 11:45 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM



Advertisments