Sign in to follow this  

easy (?) recursion problem

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)
for (all children from i to numChildren)
recurse(a->child[i], 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 this post

Link to post
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 :P
told you it was easy :)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this