Hi , Inside a recursive function i have a static variable, when i return that static variable , it's value becomes zero.
count = trimWS(someXMLNode);
i get count as 0 , but when i debug countWS was 9.
Pls help me why this is happening and what is the right way to return some value from recursive function.

example :

int trimWS(xmlNode pNode) {
   static int countWS;
   //trimming leading whitespaces
   if(pNode == 0) {
        return countWS;
   }
   while(pNode->name[0] != 0) {
        if( pNode->name[0] == 9 || pNode->name[0]== 10 || pNode->name[0]== 13                     || pNode->name[0]== 32) {
                  (pNode->name)++;
                  countWS++;
       }else {
           break;
   }
   trimWS(pNode->FChild);
}

Recommended Answers

All 2 Replies

Hmm.

I think you missed out a return statement on the last line.

...
break;
}
return trimWS(pNode->FChild)
}

which means that the function is just returning something that happens to be on the stack when it finishes executing.

yeah , thanx taw ,i made it correct.its working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.