Reputation:
The value inside the for
loop nn
turns to 0
, after it exits the loop. I expected it to increment as described in the loop. How do I make nn
save it's value after exiting?
void branch(int *numbers, int left) {
Int nb, nn;
nb = 0;
nn = 0;
if (left != 0)
for (int branchn = 1; branchn <= left; branchn++) {
for (int index = 1; index <= branchn; index++) {
*(buffer + nb) = *(buffer + nb) * 10 + *(numbers + nn++);
printf("%d\n", nn);
}
nb++;
length++;
branch(numbers, left - 1);
}
else {
branchop(length--);
nb--;
}
}
Upvotes: 1
Views: 159
Reputation: 144695
Your code has multiple problems and its design is very confusing:
Int
?buffer
, length
and branchop()
?nn
and nb
are local variables with automatic storage in branch()
; each new recursive call has its own set of these variables. You might want to make these static
so they are shared by all calls, including recursive ones, but the code would become even more convoluted.I cannot provide a corrected version of the code, because you did not give any clue as to what it is supposed to achieve and it is difficult to tell from the posted fragment.
Upvotes: 3
Reputation: 4602
Actually, it works as designed.
You are recursively calling branch()
and branch()
will set nn
to 0
on startup of the method. When you finally leave the recursive branch()
, nn
will have the assigned, incremented value of inside the for()
loop.
Upvotes: 1