Path: utzoo!yunexus!telly!ddsw1!mcdchg!rutgers!mailrus!tut.cis.ohio-state.edu!EE.ECN.PURDUE.EDU!lewie From: lewie@EE.ECN.PURDUE.EDU (Jeff Lewis) Newsgroups: gnu.gcc Subject: Re: left-hand side cast Message-ID: <8811221620.AA25541@ee.ecn.purdue.edu> Date: 22 Nov 88 16:20:52 GMT Article-I.D.: ee.8811221620.AA25541 Sender: daemon@tut.cis.ohio-state.edu Distribution: gnu Organization: GNUs Not Usenet Lines: 58 Re: left-hand side casts First, the name is awkward, how 'bout we call 'em lvalue casts? (where an lvalue is as described in the C book; roughly speaking, something to which you can assign a value.) But anyway, ... > They are a documented bug. In very rare situations, they may be > used to eliminate a declared temporary by making your code less > readable and less portable. (They always have the latter effects.) I'm not attempting to justify the addition of various non-standard features to an implementation of a language, however I think the lvalue cast is getting a bad rap here. Say you needed to push arbitrary sized data onto a stack: int *sp, foo; short int *sp2, bar; unsigned char *sp3, biff; *sp++ = foo; sp2 = (short int *) sp; *sp2++ = bar; sp3 = (unsigned char *) sp2; *sp3++ = biff; sp = (int *) sp3; ... given lvalue casts: int *sp, foo; short int bar; unsigned char biff; *sp++ = foo; *((short int *) sp)++ = bar; *((unsigned char *) sp)++ = biff; even better, much more direct and descriptive of what's going on: (here caddr_t is used in the sense of any valid pointer type of proper alignment for all values to be pushed on the stack) caddr_t sp; long int foo; short int bar; unsigned char biff; *((long int *) sp)++ = foo; *((short int *) sp)++ = bar; *((unsigned char *) sp)++ = biff; Now, obviously this isn't the kind of thing you're going to code everyday, but when, and if, you do, I find the lvalue cast much preferable to the indirection induced by the unnecessary temporaries. --Jeff Lewis (lewie@ee.ecn.purdue.edu, pur-ee!lewie)