我知道有一些关于p++、++p和p+1之间区别的解释,但是我还没有完全理解,特别是当它们不与函数一起使用时。
void replace(char * str, char c1, char c2){
if (*str == '\0') {
return;
}else if (*str == c1) {
printf("%c", c2);
}
else {
printf("%c", *str);
}
replace(++str, c1, c2);
}
当我使用replace(++str, c1, c2);
或replace(str+1, c1, c2);
时,它可以正常工作,但是replace(str++, c1, c2);
却不行。为什么?
str
进行增量操作,因为str
已经不再使用。通常的写法应该是replace(str + 1, c1, c2)
。 - dxiv