Correct way to cast

J

Joe

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

Ian Collins

Joe said:
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!
 
M

Malcolm McLean

Joe said:
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.
 
K

Keith Thompson

Joe said:
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.
 
C

CBFalconer

Joe said:
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?
 
C

CBFalconer

Malcolm said:
.... 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.
 
C

christian.bau

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

Pierre Asselin

Joe said:
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.
 
D

Dave Vandervies

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
 
R

Richard Heathfield

Pierre Asselin said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top