在下面的代码中:
int strlen(char *s){
char *p = s;
while(*p++ != '\0');
return p - s;
}
为什么上面的评估与此不同:
int strlen(char *s){
char *p = s;
while(*p != '\0') p++;
return p - s;
}
据我了解,表达式会先求值,然后递增。
最佳答案
在第一个代码中,无论 while()
条件为真还是假,p
都会递增。
在第二段代码中,p
仅在 while
条件为真时递增。
关于C - 指针算术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17636760/