'op=' operators

Jerry Nowlin nowlin at ihu1e.UUCP
Thu Oct 4 05:02:59 AEST 1984


<.>

 > I am not sure how K&R specify that
 >      *ptr++ += 2;
 > should be evaluated. Page 191 says only that the behaviour of E1 op= E2
 > is the same as E1 = E1 op E2, but does that mean in this case
 >      *ptr++ += *ptr++ + 2;
 >      *ptr += *ptr++ + 2;
 > or
 >      *ptr++ += *ptr + 2; ? (and what does the first form mean?)
 >
 > For out 4.2 bsd compiler, the last form is used, with the incrementation of
 > the pointer after the addition of 2. Is this guaranteed by the standard?
 >               Robert

(I hope you meant to use = instead of += in your attempts at clarification.)

Excuse me, but this article reminds me of "Old McDonald Had a Farm."

     With a ++ here, and a ++ there.
     Here a +.  There a +.
     Everywhere a ++.

I think there should be a rule of style that says "If you can't figure out
what a statement will do without trying it, rewrite it!" How about scanning:

     *ptr += 2;
     ptr++;

Does anybody have a shred of doubt what that will do?  If so try this:

     *ptr = *ptr + 2;
     ptr = ptr + 1;

I certainly wouldn't go as far as the last example to make my code readable,
but the "experts" that think up these clever little statements that take
longer to decipher (six months down the road when one of them backfires) than
rewriting the entire program should be forced to use COBOL for the rest of
their lives!

Jerry Nowlin
ihnp4!ihu1e!nowlin



More information about the Comp.lang.c mailing list