472,373 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

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 2490
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: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.