this is my first post.
I have this function for reversing a string in C that I found.
void reverse(char* c) {
if (*c != 0) {
reverse(c + 1);
}
printf("%c",*c);
}
It works fine but if I replace:
reverse(c + 1);
with:
reverse(++c);
the first character of the original string is truncated. My question is why would are the statements not equivalent in this instance?
Thanks
Because c + 1 doesn't change the value of c, and ++c does.
Let's expand on Fred's answer just a bit. ++c is equivalent to c = c+1, not c+1. If you replace the line reverse(c+1) with reverse(++c), then c is changed. This doesn't matter as far as the recursive call is concerned (why?) but means c is pointing somewhere new in the printf.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With