www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/06/14/15:50:22

From: j DOT aldrich6 AT genie DOT com
Message-Id: <199606141947.AA209681649@relay1.geis.com>
Date: Fri, 14 Jun 96 19:15:00 UTC 0000
To: djgpp AT delorie DOT com
Mime-Version: 1.0
Subject: Re: Is post-increment a pre-in

Reply to message 3905155    from GRENDEL AT ANANK on 06/12/96  3:39PM


>    default:
>      len += CharWidth(*walker++, curFont); // Here, the walker variable has
>                                              // been everytime incremented
>                                              // TWICE, not ONCE as it was
>                                              // meant to be - what is wrong?
>   }

I can almost guarantee that your problem results from the fact that
CharWidth is a macro, not a function.  Examine the header file that
CharWidth is declared in carefully.  This is a common pitfall with
macros, as illustrated by the following:

#define UMAX(a,b)    ((a)>(b)?(a):(b))

Now, suppose you write a line like this:

x = UMAX(foo++, ++bar);

Look what it gets expanded to:

x = ((foo++)>(++bar)?(foo++):(++bar));

See the problem?  No matter the result of the expression, either foo or bar
gets incremented twice.  Stuff like this drives programmers nuts.  :)

John

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019