Must casting destroy lvalueness?

Wayne Throop throopw at dg_rtp.UUCP
Tue Oct 21 07:48:45 AEST 1986

> NET-RELAY.ARPA>@brl-smoke.ARPA (Scott)

> I'm relatively new to info-c and don't know if you've had this debate
> so just banish me to the archive if you have.

It has come around before.

> C pedants claim that casting destroys lvalueness.  Their argument is
> essentially that they can imagine a machine on which casting forces
> the use of a temp so lvalueness is gone.

Not that they can imagine such a machine.  That such machines actually
exist.  I am editing on one now.

> C users, on the other hand, find they have to program real machines not
> hypothetical ones and that almost all of these real machines don't use a 
> temp when casting.  For example, a useful and readable way to move a pointer 
> through a buffer containing a mixture of objects of different sizes is 
>                        ((OBJECT)pointer)++
> This construct is disallowed by Harbison's compiler. 

Not really very readible.  Let's take a similar example.  Would you

        int i;

to do anything sensible?  If so, why?  If not, why should the pointer
case work sensibly?  After all, there *are* machines upon which integers
and shorts "mean the same thing", so the cast and increment could work
sometimes, right?

> I fear that the C standards committee is going to take away such practical 
> constructs and turn production quality C compilers into academic quality 
> ones.  Who knows, there may develop a brisk business in C compilers that 
> promise NOT to be standard conforming.

Only among those who don't care about portability or maintainability.

> How sayeth the C standard committee?  How sayeth the users?

I sayeth that if thou wishest to taketh an object as a different typeth,
thou mayest do so.  However, casts are not the way to do this in C, and
the practice is not portable.  If you must take the bits of one pointer
type as being those of another pointer type, use

                (*((some_type **)&p))++

or use unions like God intended.  Don't try to pervert casts to do
something they weren't intended for.  Casts convert, unions take-as.
The take-as operation is inherently non-portable.

"Thank you for your support."
Wayne Throop      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw

More information about the Comp.lang.c mailing list