473,473 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Correct way to cast

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.
Mar 14 '07 #1
9 2589
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.
Mar 14 '07 #2
"Joe" <no@to.spam.comwrote 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
Mar 14 '07 #3
Joe <no@to.spam.comwrites:
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. 8-)}

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) ks***@mib.org <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"
Mar 14 '07 #4
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

Mar 15 '07 #5
On Mar 14, 10:45 pm, Keith Thompson <k...@mib.orgwrote:
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.

Mar 15 '07 #6
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

Mar 15 '07 #7
Joe <no@to.spam.comwrote:
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
Mar 15 '07 #8
In article <ba********************************@4ax.com>,
Joe <no@to.spam.comwrote:
>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 dj******@csclub.uwaterloo.ca
[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
Mar 15 '07 #9
Pierre Asselin said:
Joe <no@to.spam.comwrote:
>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.
Mar 15 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
0
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
7
by: tjonsek | last post by:
I'm building a string to be used as the body of an email. I'm getting this error message. "Input String Not In Correct Format". I've checked several newsgroups, however, for the posts I found...
5
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
1
by: cmdolcet69 | last post by:
How can i cast down a word16 (4 bytes) rather than just a byte. You need to cast it to the correct type.
5
by: =?Utf-8?B?U3VzaGlTZWFu?= | last post by:
Hello. I have a problem with getting short value from 2 byte array. I have this code. There are 2 short values in bytes. byte cast = { 18, 152, 00, 80 }; Int32 port = BitConverter.ToInt16(cast,...
4
by: Akshay Loke | last post by:
Hi all, I have this function from a class MFnDagNode, addChild( MObject & child, unsigned int index = kNextPos, bool keepExistingParents = false ); which takes an object reference as first...
13
by: biplab | last post by:
for(i=0;i<256;i++) { hist=0; } for(i=0;i<height;i++) { for(j=0;j<width;j++) { k=(long int)rgb1; hist=hist+1;
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.