Lamefif said:
can anyone explain the output of this function.
im having trouble comprehending it
void ret_str(char* s)
{
if(*s != '\0')
//{
cout<<*(s) ;
ret_str(s+1);
//cout<<*(s) ;
//}
}
It should just output a string.
char* s is a char pointer, in this case it will point to the beginning of a
c-style string. An array of characters terminated by a null \0.
Now, in your case, as it is, it will print the string forwards. If the
character that s is pointing to is not the end, it will output the
character, then call itself with the pointer incremented by 1.
For example, say the string is:
"hello" which would consist of 'h' 'e' 'l' 'l' 'o' '\0'
First time it's called s is pointing to the 'h'. Since it's not \0 it
outputs the 'h', then calls itself with s+1, or pointing to the 'e' This
continues until the null character is reached where it simply returns.
If the cout is after the recursion call, then it would print the string
backwards. An excercise to the reader to determine why.