Mail Archives: djgpp/1996/06/14/12:20:11
Xref: | news2.mv.net comp.os.msdos.djgpp:4962
|
From: | Mark Habersack <grendel AT ananke DOT amu DOT edu DOT pl>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Is post-increment a pre-increment?
|
Date: | Wed, 12 Jun 1996 21:39:52 +0200
|
Organization: | Adam Mickiewicz University
|
Lines: | 54
|
Message-ID: | <Pine.NEB.3.93.960612212436.3438B-100000@ananke.amu.edu.pl>
|
NNTP-Posting-Host: | ananke.amu.edu.pl
|
Mime-Version: | 1.0
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Hi,
Today, debugging some code, I have found strange behaviour of the
post-increment operator. Maybe it is a bug (or "hidden feature" how Microsoft
would describe it) and maybe just a side effect which I don't understand.
Anyway, here's what was going on:
I was writing the code using TWS library and wanted to calculate the width
in pixels of the string with embedded formatting codes (skipping them of
course). To achieve this I had to add the width of every individual character
to the total value. The algorithm presented here is not entirely what I used
in my code, but illustrates the problem. I used the following approach (the
code was in C++ module):
int len = 0;
char* walker = text; // Text is a char* argument passed to the func
while(*walker)
switch(*walker)
{
case '%':
switch(*(++walker))
{
.
. // Do the processing of formatting codes
.
}
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?
}
Later on I changed the offending line to
len += CharWidth(*walker, curFont);
walker++;
and everything worked just fine. Where lies the error - is it my ignorance or
a bug or side effect?
N.B. Composing this message a thought has crossed my mind: maybe it is a case
of operator precedence - I always keep forgetting about this stuff? If so I'm
just dumb - forgive me!
Greetings, Mark
/*******************************/
/** So here I am once more... **************************************/
/** When you grown up and leave your playground, where you kissed **/
/** your Prince and found your Frog - remember the Jester that **/
/** showed you tears, a Script for Tears... ************************/
/*********************************************/
- Raw text -