Casting a postdecrement operand

Wayne Throop throopw at dg_rtp.UUCP
Sun Jun 8 07:41:55 AEST 1986


> david at sun.UUCP (David DiGiacomo)
>> chris at 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      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw



More information about the Comp.lang.c mailing list