Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!ll-xn!mit-eddie!genrad!decvax!ucbvax!cartan!brahms!desj From: desj@brahms (David desJardins) Newsgroups: net.lang.c Subject: Re: Must casting destroy lvalueness? Message-ID: <61@cartan.Berkeley.EDU> Date: Thu, 23-Oct-86 22:11:12 EDT Article-I.D.: cartan.61 Posted: Thu Oct 23 22:11:12 1986 Date-Received: Sat, 25-Oct-86 01:37:29 EDT References: <4617@brl-smoke.ARPA> <657@dg_rtp.UUCP> Sender: daemon@cartan.Berkeley.EDU Reply-To: desj@brahms (David desJardins) Organization: Math Dept. UC Berkeley Lines: 55 Summary: More reasons for casting lvalues. In article <657@dg_rtp.UUCP> throopw@dg_rtp.UUCP (Wayne Throop) writes: >Let's take a similar example. Would you expect > > int i; > ((short)i)++; > >to do anything sensible? If so, why? In my opinion this should have the result * ((short *) &i) = (short) i + 1; Obviously the result of this operation is machine-dependent (since the effect of casting int to short is machine-dependent). But on an appropriate machine this does indeed have not only a sensible but a *useful* effect -- it will increment the low bytes of i without carrying into the high bytes. At any rate the casting of int to short performs a fundamentally different operation than does casting of pointers (in Wayne's terminology, the former "converts" and the latter "takes-as"), and so it is not necessary for one to make sense in order for the other to be allowed. Note also that on most machines the statements int *p; ((int) p)++; makes sense (it increments the address referenced by p by the addressing unit of the machine). In fact this is arguably the correct way to use the value produced by 'sizeof'; (int) p += sizeof (foo); makes sense on any machine where 'sizeof' gives results in multiples of the addressing unit of the machine, whereas the more common alternative p = (int *) ((char *) p + sizeof (foo)); is both clumsier and makes the unnecessary assumption that sizeof (char) == 1. Another justification for casting of lvalues is the case of register variables. In this case Wayne's alternative syntax doesn't work: register int *p; (* ((foo **) &p))++; <== ERROR But the idea of casting (or "taking-as") the pointer p as a pointer to foo is still perfectly valid, and the proposed syntax ((foo *) p)++; still makes sense, and can be understood and implemented by a compiler. -- David desJardins