C compiler weirdness?

michael sweet sweetmr at SCT60A.SUNYCT.EDU
Tue Apr 9 08:32:50 AEST 1991


> only to discover that the C compiler does not increment pc until after the
> function call returns in the following statement:
>           (*(*pc++))();

The time of increment/decrement IS compiler dependent.  The only constraint
with ++/-- is that '++i' increments 'i' before using that value, and 'i--'
after the valuye is used.  This causes MUCH heartache in the above code and
the following (which I see all too often, and should NEVER be used if you want
to port code to a different machine!)

 while (*s)
  *s = *++s;

(or similar...)  What the person probably wants to do is delete a character
from a string (starting at 's'...).  The idiot assumes that '*s' is evaluated
for the assignment FIRST, and then 's' is incremented.  A nicer way (which
will get optimized away anyway) and MORE READABLE way is:

 while (*s)
  {
   *s = *(s+1);
   s++;
  };


The KISS philosophy is best when coding in C (keeps you from going crazy!)

 -Mike Sweet

------------------------------------------------------------------------------
"The only        TASC                      (315) 724-1100 (voice)
 truth is that   555 French Road           (315) 724-2031 (fax)
 there are no    New Hartford, NY  13413   Internet: sweetmr at sct60a.sunyct.edu
 truths, only beliefs."                    Delphi:   DODGECOLT
------------------------------------------------------------------------------



More information about the Comp.sys.sgi mailing list