In the recent post C string to uppercase in C and C++, the function:
在最近发布的C字符串到C和C ++的大写字母中,函数:
void strupp(char* beg)
{
while (*beg++ = toupper(*beg));
}
showed the undesirable result of 'OOBAR' when given 'foobar' with an answer explaining "there is no sequence point" in the expression. Now I have always been using
当给出'foobar'并且在表达式中解释“没有序列点”的答案时,显示了'OOBAR'的不良结果。现在我一直在使用
char *s1=strTo, *s2= strFrom;
while (*s2) *s1++ = *s2++;
with the understanding it means to get the value of the right part (*s2
), increment s2
; assign the obtained value to *s1
and increment s1
. But it seems that this neither contains a sequence point so that it has always worked would be coincidence (luck), which I can't believe.
理解它意味着获得正确部分的值(* s2),增加s2;将获得的值分配给* s1并增加s1。但似乎这既不包含一个序列点,所以它一直有效将是巧合(运气),我无法相信。
Anyone can help me and explain this?
有人可以帮我解释一下吗?
2 个解决方案
#1
2
The outcome of
结果
while (*beg++ = toupper(*beg));
depends on whether the LHS is evaluated first or the RHS is evaluated first.
取决于首先评估LHS还是首先评估RHS。
The outcome of
结果
while (*s2) *s1++ = *s2++;
does not depend on whether the LHS is evaluated first or the RHS is evaluated first.
不依赖于首先评估LHS还是首先评估RHS。
That is the crucial difference. Hence, lack of a sequence point matters in the first case but does not matter in the second case.
这是至关重要的区别。因此,缺少序列点在第一种情况下很重要,但在第二种情况下无关紧要。
#2
1
The result of your operation:
您的操作结果:
*s1++ = *s2++;
does not depend on when the increments are done. So the absense of the sequence point doesn't pose any problems.
不依赖于增量何时完成。因此,序列点的缺失不会造成任何问题。
#1
2
The outcome of
结果
while (*beg++ = toupper(*beg));
depends on whether the LHS is evaluated first or the RHS is evaluated first.
取决于首先评估LHS还是首先评估RHS。
The outcome of
结果
while (*s2) *s1++ = *s2++;
does not depend on whether the LHS is evaluated first or the RHS is evaluated first.
不依赖于首先评估LHS还是首先评估RHS。
That is the crucial difference. Hence, lack of a sequence point matters in the first case but does not matter in the second case.
这是至关重要的区别。因此,缺少序列点在第一种情况下很重要,但在第二种情况下无关紧要。
#2
1
The result of your operation:
您的操作结果:
*s1++ = *s2++;
does not depend on when the increments are done. So the absense of the sequence point doesn't pose any problems.
不依赖于增量何时完成。因此,序列点的缺失不会造成任何问题。