问题描述
#include <stdio.h>
int main() {
char *c = "test";
while (*c != '\0') {
printf("%c",*c);
*c++; // or c++ both produced same result. ie test
}
return 0;
}
因为 *c++ 应该增加值,而 c++ 应该增加指针地址。但是两者都是递增指针为什么?
解决方法
这是因为运算符优先级 ++ 的优先级高于 *
,但两者都增加指针为什么?
那是因为所谓的运算符优先级。看看here
如果你想增加价值,那么你应该这样做
strip
作为一点建议,您的代码更像是“C”而不是“C++”。尽可能避免使用原始指针并使用 iostream 库而不是 (*c)++ //increment the value at address c