I am having trouble understanding the following code. Although I tried to debug it for some time it seems that I can't understand the exact mechanics of the code and I am feeling stuck. Any help will be deeply appreciated.
edit: Well my problem is mostly located on the recursive function and how it will know when to stop calling it self.
int main(){
char word[10]="abcdfb";
doSomethingELSE(word);
printf("%s",word);
return 0;
}
void doSomethingELSE(char * w){
static char * s =NULL;
char t;
if(!s){
s=w;
}
if(*w==0)return;
doSomethingELSE(w+1);
if(s>w)return;
if(*w -*s==1){
t=*s;
*s=*w;
*w=t;
}
s++;
return;
}
The recursive function calls stop after
doSomethingELSE(w+1)
gets "bcdfb", "cdfb", "dfb", "fb", "b", "" ->if(w[0] == '\0') return
. This places an execution stack of function calls for eachchar
, going backwards. Thestatic
variables
goes forward. After cleaning-up, it is more clear what happens: