简体   繁体   English

而哨兵变量与后递增运算符

[英]while sentinel variable with post-increment operator

what's the difference between: 之间有什么区别:

while(*s++ != '\0') {}

and

while(*s != '\0') {
   s++;
}

s is a char * . s是一个char * The latter works OK. 后者工作正常。 but at the end of first loop, *s is not equal to '\\0' . 但在第一个循环结束时, *s不等于'\\0'

In case of 的情况下

 while(*s++ != '\0') {}

the increment is done as a post increment operator, in the condition-check statement itself. 在条件检查语句本身中,该增量是作为后增量运算符完成的。 In this case, the value change (increment) is the side effect after the value computation for the operator. 在这种情况下,值变化(增量)是对操作员进行值计算之后的副作用。 Thus, after the value is used (in comparison), s gets incremented. 因此,在使用该值(比较)之后, s会增加。

On the other hand, 另一方面,

while(*s != '\0') {
   s++;
}

the increment takes place as post-increment inside the conditional block, which will only execute if the condition is TRUTHY. 增量在条件块内作为后增量发生,仅在条件为TRUTHY时才执行。 Once the condition is evaluated to be false, s is not incremented. 一旦条件被评估为假, s就不会增加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM