Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!mcnc!rti-sel!dg_rtp!throopw From: throopw@dg_rtp.UUCP (Wayne Throop) Newsgroups: net.lang.c Subject: Re: Casting a postdecrement operand Message-ID: <392@dg_rtp.UUCP> Date: Sat, 7-Jun-86 17:41:55 EDT Article-I.D.: dg_rtp.392 Posted: Sat Jun 7 17:41:55 1986 Date-Received: Sun, 8-Jun-86 09:02:36 EDT References: <114@romp.UUCP> <2842@utcsri.UUCP> <378@dg_rtp.UUCP> <1764@umcp-cs.UUCP> <3925@sun.uucp> Lines: 57 Summary: gives motivation, and the general case of "mentioning things twice" > david@sun.UUCP (David DiGiacomo) >> chris@maryland.UUCP (Chris Torek) >> ((struct abc *)cbap)++; >>is not legal, but >> (*(struct abc **)&cbap)++; >>is (again, if cbap is addressable). What it means is machine >>dependent! > This is disgusting... why not use > cbap = (struct cba *) ((caddr_t) cbap + sizeof(struct abc)); Because the latter construct mentions "cbap" twice. Thus, if this type of expression were embedded in a macro, "cbap" had better not contain side effects. Also, if "cbap" is difficult or complicated (you know, something like (*f1(a[s1][e1+e2],f2())) or worse), then expanding it twice might be undesirable even if it *were* side effect free. In short, it is indeed *useful* to do such things, just as things like (i += 2) are useful... but nobody has found a way to give the language sensible, consistent semantics if such a thing is allowed. Now, I know what you're thinking. You're thinking, ok, so why not cbap += (sizeof(struct abc) / sizeof(struct cba)); (assuming that the division has no remainder, of course, as might often be the case, or as may be arranged to be the case.) Well, because this isn't a post-increment construct, and that is what the whole thing started out over... somebody noticed that the pcc allows a post-increment on a cast of a pointer for some machines. The moral is, it just isn't possible to avoid mentioning an instance twice in all possible perverse cases. Sad but true. If it is really important to mention an instance only once, and then get at that instance many times, a binding-by-reference would work: { T *p = &reference_of_type_T_that_i_only_want_to_mention_once; ... *p ... *p ... *p ... } Now you can use the object that the complicated reference mentions as an lvalue many times, yet only evaluate it once. Yes, you are right... it *isn't* pretty. But sometimes a Programmer's gotta do what a Programmer's gotta do, a-yuh. -- "Now men, we've got uh new villuge ta raid. I want ya ta kill all the men, round up all the livestock, an' ravage all the wimmun. An' fer *God*'s *sake* get it *right* this time!" Rich Little as John Wayne, in the role of Ghengis Khan. -- Wayne Throop !mcnc!rti-sel!dg_rtp!throopw