The advantage of tail recursion is that we call the function (recursion) again in the last action of the code, so the stack variables doesn't need to be saved.
So here do this will act the same? and I can't be sure that the string will stay In the stack and not will be cleaned?
void foo(char* c){
c[0]='b';
printf("%s",c);
}
void foo2(){
char str[]="aaa";
foo(str);
}
int main(){
foo2();
}
or I can be sure that the string in foo2 will stay until the function ends (gets to the end scope).
I am asking if str that is local variable of function foo2 will still be available, or if this local variable (str) will be cleaned because no more actions to do in this function foo2 after calling foo(str).
I want to understand if could be that in foo I will try to use a string that is not available anymore.
Output:
baa
By just this example I can see that str didn't disappear until got to the end of foo2 scope, but I want to know if this will be like this every time, and if yes how this makes sense with the idea of tail recursion that also calls a function in the last action.
cvariable infoo2will obviously be modified by callingfoo(c). I'm not really getting what you're asking here.foo2. It just calledprintfdirectly frommain. With -O2maincalledfoo2butfoo2never calledfoo. Justprintf. With -O1 and -O0 bothfoo2andfoowas called.