473,698 Members | 2,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

double casts

Hi,

Those familiar with Windows programming may be familiar with the windowsx.h
header. In this header macros exist that use double casts, like so (hope
this is readable):

#define ComboBox_LimitT ext(hwndCtl,cch Limit) \

((int)(DWORD)Se ndMessage((hwnd Ctl),CB_LIMITTE XT,(WPARAM)(int )(cchLimit),0))

Why is this? Does this help in typechecking? (I would think not).

Thanks for the info,

--
Martijn
http://www.sereneconcepts.nl
Nov 13 '05 #1
20 2308
On Sun, 2 Nov 2003 13:33:05 +0100, "Martijn"
<su************ *********@hotNO FILTERmail.com> wrote in comp.lang.c:
Hi,

Those familiar with Windows programming may be familiar with the windowsx.h
header. In this header macros exist that use double casts, like so (hope
this is readable):
Your text is readable, the concept of what the code is doing is not.
#define ComboBox_LimitT ext(hwndCtl,cch Limit) \

((int)(DWORD)Se ndMessage((hwnd Ctl),CB_LIMITTE XT,(WPARAM)(int )(cchLimit),0))

Why is this? Does this help in typechecking? (I would think not).
Casts never help in type checking, they specifically exist to defeat
type checking.
Thanks for the info,


For a variety of reasons, Windows header files are a complete and
total mess. If you really want to delve into the details, I'd suggest
a Windows programming group or one of Microsoft's support groups.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #2
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:on******** *************** *********@4ax.c om...
On Sun, 2 Nov 2003 13:33:05 +0100, "Martijn"
<su************ *********@hotNO FILTERmail.com> wrote in comp.lang.c:
Hi,

Those familiar with Windows programming may be familiar with the windowsx.h
header. In this header macros exist that use double casts, like so (hope
this is readable):


Your text is readable, the concept of what the code is doing is not.
#define ComboBox_LimitT ext(hwndCtl,cch Limit) \

((int)(DWORD)Se ndMessage((hwnd Ctl),CB_LIMITTE XT,(WPARAM)(int )(cchLimit),0))

Why is this? Does this help in typechecking? (I would think not).


Casts never help in type checking, they specifically exist to defeat
type checking.
Thanks for the info,


For a variety of reasons, Windows header files are a complete and
total mess. If you really want to delve into the details, I'd suggest
a Windows programming group or one of Microsoft's support groups.


That wasn't his question.

His question is this: When is it necessary, if ever, to use
a double cast?

For example:

1. (WPARM)(int)(cc hLimit)

2. (WPARM)(cchLimi t)

Are these two expressions *always* equivalent? Is there ever
a situation where the intermediate cast (int) is necessary
to get a "correct" final cast to (WPARM)?

Just ignore whatever "WPARM" is supposed to be and think
about the generic question of double casting versus single casting.
Don't get distracted about Windows. This is really a C question.

For you C gurus out there, what is the definitive answer?

Nov 13 '05 #3
xarax <xa***@email.co m> scribbled the following:
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:on******** *************** *********@4ax.c om...
On Sun, 2 Nov 2003 13:33:05 +0100, "Martijn"
<su************ *********@hotNO FILTERmail.com> wrote in comp.lang.c:
> Hi,
>
> Those familiar with Windows programming may be familiar with the windowsx.h
> header. In this header macros exist that use double casts, like so (hope
> this is readable):
Your text is readable, the concept of what the code is doing is not.
> #define ComboBox_LimitT ext(hwndCtl,cch Limit) \
>
> ((int)(DWORD)Se ndMessage((hwnd Ctl),CB_LIMITTE XT,(WPARAM)(int )(cchLimit),0))
>
> Why is this? Does this help in typechecking? (I would think not).


Casts never help in type checking, they specifically exist to defeat
type checking.
> Thanks for the info,


For a variety of reasons, Windows header files are a complete and
total mess. If you really want to delve into the details, I'd suggest
a Windows programming group or one of Microsoft's support groups.

That wasn't his question. His question is this: When is it necessary, if ever, to use
a double cast? For example: 1. (WPARM)(int)(cc hLimit) 2. (WPARM)(cchLimi t) Are these two expressions *always* equivalent? Is there ever
a situation where the intermediate cast (int) is necessary
to get a "correct" final cast to (WPARM)? Just ignore whatever "WPARM" is supposed to be and think
about the generic question of double casting versus single casting.
Don't get distracted about Windows. This is really a C question. For you C gurus out there, what is the definitive answer?


I am not a C guru and cannot answer the OP's question, but I can say
for certain that in general, an expression of the type (A)(B)something
is not the same thing as (A)something.
Proof:
int i;
int *p1 = (int *)&i;
int *p2 = (int *)(long)&i;
p1 is guaranteed to store i's address. p2 is not.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"We're women. We've got double standards to live up to."
- Ally McBeal
Nov 13 '05 #4
Joona I Palaste <pa*****@cc.hel sinki.fi> writes:
[...]
I am not a C guru and cannot answer the OP's question, but I can say
for certain that in general, an expression of the type (A)(B)something
is not the same thing as (A)something.
Proof:
int i;
int *p1 = (int *)&i;
int *p2 = (int *)(long)&i;
p1 is guaranteed to store i's address. p2 is not.


Ok, that's a good example of when a double cast is harmful. Are there
any cases where it's helpful -- i.e., where (A)(B)something differs
from (A)something *and* (A)(B)something does something useful that
(A)something does not. In other words, if I see (A)(B)something in
source code, can I always delete the "(B)" without breaking anything?

Here's one example (assume 8-bit char):

unsigned int n = 257;
float f0 = (float)n; /* f0 == 257.0 */
float f1 = (float)(unsigne d char)n; /* f1 == 1.0 */

Are there any examples not involving deliberate loss of information?

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #5
xarax wrote:
"Jack Klein" <ja*******@spam cop.net> wrote in message
"Martijn" <su************ *********@hotNO FILTERmail.com> wrote:

Those familiar with Windows programming may be familiar with
the windowsx.h header. In this header macros exist that use
double casts, like so (hope this is readable):

#define ComboBox_LimitT ext(hwndCtl,cch Limit) \

((int)(DWORD)Se ndMessage((hwnd Ctl),CB_LIMITTE XT,(WPARAM)(int )(cchLimit),0))

Why is this? Does this help in typechecking? (I would think
not).


Casts never help in type checking, they specifically exist to
defeat type checking.

For a variety of reasons, Windows header files are a complete
and total mess. If you really want to delve into the details,
I'd suggest a Windows programming group or one of Microsoft's
support groups.


That wasn't his question.

His question is this: When is it necessary, if ever, to use
a double cast?

For example:

1. (WPARM)(int)(cc hLimit)
2. (WPARM)(cchLimi t)

Are these two expressions *always* equivalent? Is there ever
a situation where the intermediate cast (int) is necessary
to get a "correct" final cast to (WPARM)?

Just ignore whatever "WPARM" is supposed to be and think
about the generic question of double casting versus single
casting. Don't get distracted about Windows. This is really
a C question.

For you C gurus out there, what is the definitive answer?


One possibility that occurs to me is when you don't know the
signedness of the original item. Thus you might need:

(size_t) ((unsigned char) ch)

which could avoid generating a large number from '\0xff', say.

However the essential illegitimacy of windows headers remains :-)

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #6
On Sun, 02 Nov 2003 20:45:22 +0000, Keith Thompson wrote:
Joona I Palaste <pa*****@cc.hel sinki.fi> writes:
[...]
I am not a C guru and cannot answer the OP's question, but I can say
for certain that in general, an expression of the type (A)(B)something
is not the same thing as (A)something.
Proof:
int i;
int *p1 = (int *)&i;
int *p2 = (int *)(long)&i;
p1 is guaranteed to store i's address. p2 is not.


Ok, that's a good example of when a double cast is harmful. Are there
any cases where it's helpful -- i.e., where (A)(B)something differs
from (A)something *and* (A)(B)something does something useful that
(A)something does not. In other words, if I see (A)(B)something in
source code, can I always delete the "(B)" without breaking anything?

Here's one example (assume 8-bit char):

unsigned int n = 257;
float f0 = (float)n; /* f0 == 257.0 */
float f1 = (float)(unsigne d char)n; /* f1 == 1.0 */

Are there any examples not involving deliberate loss of information?


I have some code that uses a few double casts, but they rely on
implementation-defined behavior -- specifically that an unsigned
integer cast to a signed integer becomes the signed integer value
with the same bit value as the unsigned integer.

i.e.:
unsigned char uc = 255;
signed char sc = (signed char)uc;
/* sc has value -1 */

The code does things like this:

short s;
unsigned char uca, ucb;
...
++uca; /* increment with intended wrap from 255 to 0 */
...
--ucb; /* decrement with intended wrap from 0 to 255 */
...
s = (short)(signed char)uca + (short)(signed char)ucb;

The (signed char) casts are necessary to force the two
values being added into the range -128..+127 and the (short)
casts are necessary to prevent problems when the sum would
overflow a signed char.
Nov 13 '05 #7
Sheldon Simms <sh**********@y ahoo.com> wrote in message news:<pa******* *************** ******@yahoo.co m>...
....
unsigned char uc = 255;
signed char sc = (signed char)uc;
/* sc has value -1 */


There is no standard guarantee that sc has the value -1.

If 255 <= SCHAR_MAX, then sc gets the value 255, otherwise 255 is
converted in an implementation defined way. [C99 allows an
implementation defined signal to be raised, although that's highly
unlikely.]

--
Peter
Nov 13 '05 #8
On Sun, 02 Nov 2003 18:05:24 -0800, Peter Nilsson wrote:
Sheldon Simms <sh**********@y ahoo.com> wrote in message news:<pa******* *************** ******@yahoo.co m>...
...
unsigned char uc = 255;
signed char sc = (signed char)uc;
/* sc has value -1 */


There is no standard guarantee that sc has the value -1.

If 255 <= SCHAR_MAX, then sc gets the value 255, otherwise 255 is
converted in an implementation defined way. [C99 allows an
implementation defined signal to be raised, although that's highly
unlikely.]


Thanks for pointing that out, even though I had already said that
my code relied on implementation defined behavior. Since you didn't
bother quoting it, here's what I said:
I have some code that uses a few double casts, but they rely on
implementation-defined behavior


-Sheldon

Nov 13 '05 #9
"Sheldon Simms" <sh**********@y ahoo.com> wrote in message
news:pa******** *************** *****@yahoo.com ...
On Sun, 02 Nov 2003 18:05:24 -0800, Peter Nilsson wrote:
Sheldon Simms <sh**********@y ahoo.com> wrote in message news:<pa******* *************** ******@yahoo.co m>...
...
unsigned char uc = 255;
signed char sc = (signed char)uc;
/* sc has value -1 */


There is no standard guarantee that sc has the value -1.

If 255 <= SCHAR_MAX, then sc gets the value 255, otherwise 255 is
converted in an implementation defined way. [C99 allows an
implementation defined signal to be raised, although that's highly
unlikely.]


Thanks for pointing that out, even though I had already said that
my code relied on implementation defined behavior.

Since you didn't bother quoting it, here's what I said:


It's not that I didn't bother; I wanted to isolate the specific code that my
comment referred to.
I have some code that uses a few double casts, but they rely on
implementation-defined behavior
Continued by...
--specifically that an unsigned
integer cast to a signed integer becomes the signed integer value
with the same bit value as the unsigned integer.


Which isn't actually enough to give sc the value -1, even on an 8 bit char
implementation. You need the further assumption of two's complement.

Even implementation specific code is worth analysing from the point of view
of trying to make it (and similar code) non implementation dependant. To do
that, we (or at least I) need to discus what those dependancies are, i.e.
what the standards guarantee, and what they don't. For me, that is the whole
point of comp.lang.c.

In any case, I apologise if you feel I misrepresented you by taking your
post too far out of context.

--
Peter
Nov 13 '05 #10

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

Similar topics

6
7325
by: Simon | last post by:
Hi all I am writing a small app that uses real numbers all over the place for calculations. But when it comes to displaying values it is better to only display an it, (especially when it comes to % values and so on). so I first thought that the best way would be... ....
4
4180
by: Michael Mair | last post by:
Hi there, actually, I have posted the same question in g.g.help. As there were no answers, I am still not sure whether this is a bug or only something open to the compiler that is seemingly inconsistent or whether my understanding of C is not complete enough. I would appreciate answers or pointers to answers very much.
8
13002
by: Quinn Kirsch | last post by:
Hi all, the following c# example baffles me. my understanding of the managed code of visual .net is that all casts like this would be safe, but this example seems to contradict this notion. if you look at the debugger info, the double dou is half filled w/ garbage bits, instead of those bits being zeroed out appropriately. using System; namespace decimaltodouble {
69
5569
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
20
2323
by: mikael.liljeroth | last post by:
Why do I get NO as result from this ?? double du = (double)3.1415; du = ceil(du); if(du == (double)4) printf("YES\n"); else printf("NO\n");
16
11267
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
2
13444
by: tkirankumar | last post by:
Hi all, uname -a SunOS cbmrsd1a1 5.10 Generic_118833-17 sun4us sparc FJSV,GPUZC-M g++ -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.10/3.3.2/specs Configured with: ../configure --with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/ld --disable-nls
17
2503
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
116
35873
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
0
8676
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8898
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3051
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 we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.