Path: utzoo!utgpu!water!watmath!clyde!burl!codas!killer!jfh From: jfh@killer.UUCP (The Beach Bum) Newsgroups: comp.lang.c Subject: Re: lvalues (was Re: Question about type casting) Summary: Slightly more complicated that it really is. Message-ID: <2975@killer.UUCP> Date: 18 Jan 88 06:25:04 GMT References: <546@xyzzy.UUCP> <5080012@hpfcdc.HP.COM> <10227@mimsy.UUCP> Organization: Big "D" Home for Wayward Hackers Lines: 59 In article <10227@mimsy.UUCP>, chris@mimsy.UUCP (Chris Torek) writes: > In some article somewhere, someone writes: > >>I believe that Standard C does not allow type casting on the left of the > >>= sign. > > In article <5080012@hpfcdc.HP.COM> boemker@hpfcdc.HP.COM (Tim Boemker) writes: > >How about this: > > > >int i; char *c; > >c = (char *) &i; > >* (int *) c = 0; > > It is not portable, but it is legal. I believe it used to be portable. Now that X3J11 has mangled the language, it isn't guaranteed to be portable. If I recall correctly, a pointer to any object can _portably_ be converted to a pointer to a smaller object and back again. Thus, int i; char * cp; cp = (char *) &i; * (int *) cp = 0; is portable, but int *ip; char c; ip = (int *) &c; * (char *) ip = '0'; is not. > The result of a cast is an `rvalue' (R for Right: or in other words, > the kind of thing one finds on the right hand side of an assignment). > Assignments may be made only to `lvalues' (L for Left: the kind of > thing found on the left hand side of an assignment). The C indirection > operator `*' takes an rvalue and yeilds an lvalue, hence the result > of `* (int *) c' is an lvalue. > -- > In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Chris, that's some good voodoo. A cast takes an object of whatever type it happens to be, and whatever state of (l|r)value-ness and changes it's type, but not it's (l|r)value-ness. The difference between lvalue expressions and rvalue expressions is more obvious in a language such as BLISS with all those silly dots. . . . . . The only restriction on what can be on the left hand side of an `=' is having an address and a type, or being finaglable into having both. I can cast an obvious rvalue like `2' into an int * with ((int *) 2). This thing can now appear on either side - *((short *) 2) = *((short *) 4); - John. -- John F. Haugh II SNAIL: HECI Exploration Co. Inc. UUCP: ...!ihnp4!killer!jfh 11910 Greenville Ave, Suite 600 "Don't Have an Oil Well? ... Dallas, TX. 75243 ... Then Buy One!" (214) 231-0993 Ext 260