Can't incremen int inside funciton call -- bug?

Daniel Keep daniel.keep.lists at gmail.com
Tue May 29 04:53:43 PDT 2007



Aldarris wrote:
> Hello.
> 
> I have a nested function that iterates through a tree recursively.
> here is it code:
> 
> void recursiveXMLTreeIteration(XMLNode currentNode, int treeLevel)
> {
> 	writefln(treeLevel);
> 	tabOffset.length = 0;
> 	for(int i = 0; i < treeLevel; i++)
> 		{tabOffset ~= "\t";}
> ........			
> 	foreach(XMLNode childNode; currentNode.getChildrenList())
> 		{recursiveXMLTreeIteration(childNode, treeLevel++);}
> }
> 
> recursiveXMLTreeIteration(this, 0);
> 
> Does not work as expected, in a tree of a root node plus two child nodes it prints 0,0,1.
> 
> It treeLevel++; is moved outside, works as expected:
> ........			
>         treeLevel++;
> 	foreach(XMLNode childNode; currentNode.getChildrenList())
> 		{recursiveXMLTreeIteration(childNode, treeLevel)
> }
> 
> 
> Prints 0,1,1,

It's a bug alright: in your code.

treeLevel++ is post increment.  It evaluates "treeLevel" and THEN
increments it.  You want pre increment: "++treeLevel".

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list