Re: question about cast

From: Keith Thompson (kst-u_at_mib.org)
Date: 01/16/04


Date: Thu, 15 Jan 2004 23:45:06 GMT

Horst Kraemer <horst.kraemer@epost.de> writes:
[...]
> Your compiler uses some extension to the C language. It allows
> (int)y=x because sizeof(int)==sizeof y on this platform and it refuses
> (double)y=x because sizeof(double)>sizeof y on this platform.

I don't believe the sizes of the various types have anything to do
with this.

In C, a cast is not allowed on the left side of an assignment,
regardless of the types. If a cast is being used as a value (not as
an lvalue), it's legal to convert from a pointer type to an integer
type or vice versa, whether the sizes match or not. It's not legal to
convert between pointer types and floating-point types.

I would guess that the extension implemented by whatever compiler the
OP is using (allowing a cast as an lvalue) would following the same
rules. (As I vaguely alluded to earlier, I think C++ allows casts as
lvalues. I don't know the details, which aren't topical anyway.)

Note that conversions between integers and pointers, though they're
legal, are not usually meaningful. You can convert a pointer to a
sufficiently large integer type (if there is one) and back again and
get the same pointer value. Anything else will probably give you
garbage.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"


Relevant Pages

  • Re: Why the compiler applies sign extension to unsigned data?
    ... I want the value of p to be made unsigned *before* the cast. ... want the cast to generate a sign extension. ... ULONG_PTR is ULONG and casting the pointer to ULONG_PTR first is the ... on the compiler and on the knowledge of the size of the ptr variable, ...
    (microsoft.public.development.device.drivers)
  • Re: Why the compiler applies sign extension to unsigned data?
    ... In the i386 segmented architecture, both the base and the limit from ... Every C programmer should know that you can't cast a pointer directly to ... *Why* do you care about the sign extension? ...
    (microsoft.public.development.device.drivers)
  • Re: The usage of %p in C
    ... I think the cast is unnecessary, unless int * and void * ... are of different sizes in your system... ... a value held by another pointer of any kind? ...
    (comp.lang.c)
  • Re: The usage of %p in C
    ... I think the cast is unnecessary, unless int * and void * ... are of different sizes in your system... ... a value held by another pointer of any kind? ...
    (comp.lang.c)
  • Re: [PATCH] [0/9] Use 64bit x86 machine check code for 32bit too
    ... warning: passing argument 2 of ‘strict_strtoull’ makes integer from pointer without a cast ...
    (Linux-Kernel)

Loading