# easy (?) recursion problem

This topic is 2645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi i need some help writing a proper recursion loop, but im simply to stupid to get it right

ive got an array, lets say int[10]. im trying to fill this array, and the values i want it to be filled with get calculated in a recursive loop recurse() operating on some arbitrary tree, which nodes are of the data structure A.
the data i want to retrieve is stored in nodevalue.
so what i tried was something like this (this is just pseudocode, it wont compile, so if you get what its doing, pls dont comment on any blatant mistakes)
 struct A { int nodevalue; //data int numChildren; //number of children A** ArrayOfChildren; //array with pointers to children } int val[10], *pVal = val; recurse(A *a, int* pVal) { *pVal=a->nodevalue; ++pVal; for (all children from i to numChildren) { recurse(a->child, pVal); } } 

now obviously this cant work, because as soon as the first recursion returns (when some node B has no children), pVal will reset to the element it pointed too in B's parents' stack frame...
now this seems like a super simple problem, but i cant figure out how to do this =). is there an easy way to do this without actually returning the position of pVal? (i.e. int* recurse(A *a, int* pVal) {....; return pVal}
i need the return value for something else :/.
also, i dont want the function getting crowded with variables, because the problem seems so easy actually...

##### Share on other sites
ah nvm, found it XD
all i had to do was pass along the adress of the pointer, since that one doesnt change between stack frames, that solved the problem
told you it was easy

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633271
• Total Posts
3011161
• ### Who's Online (See full list)

There are no registered users currently online

×