Must casting destroy lvalueness?

Lambert Meertens lambert at mcvax.uucp
Thu Nov 6 18:07:22 AEST 1986


I still don't get what is so fundamentally wrong with the following
*addition* to C, which it seems to me that desj at brahms (David desJardins)
is arguing for:

    A cast (T)E in a context where an lvalue is required
    stands for *(T *)&(E) and so is lawful if the latter
    would be allowed here.

For example, this would then make the following lawful:

    int i;
    char *p = (char *)&i;
    ++(int)*p;

At least, in `my' cc (BSD 4.3) everything works as expected if I

    #define Lcast(T,E) (*(T*)&(E))

and then use

    ++Lcast(int, *p);

(I don't know if this is allowed by the ANSI C draft.)  Note that I am not
particularly arguing in favour of this extension, the fact being that this
is one of the areas where I can live with current C.  But neither do I see
a reason to get upset about it.  It allows you to write pretty meaningless
things, but that is the case already for current casts.  One thing that is
certain is that it does not invalidate existing code.  Also, it seems
consistent to me with the design philosophy of C: start by putting some
very confusing things in (pointers vs. arrays, here pointers vs. lvalues),
then legitimate some of the most obvious unambiguous mistakes.

-- 

Lambert Meertens, CWI, Amsterdam; lambert at mcvax.UUCP



More information about the Comp.lang.c mailing list