it should set the current character to next character. For example:
while( *foo ) {
if(baa(*foo)) *foo++ = *++foo;
foo++;
}
But I get the following errors:
error: operation on ‘foo’ may be undefined [-Werror=sequence-point]
cc1: all warnings being treated as errors
Can anyone explain why that? that isn’t valid C syntax?
Let’s take a closer look at this expression:
*foo++evaluates as*(foo++)(postfix++has higher precedence than unary*); you take the current value offooand dereference it, and advancefooas a side effect.*++fooevaluates as*(++foo)(both unary*and++have the same precedence, so they are applied left-to-right); you take the value offoo + 1, dereference the result, and then advancefooagain as a side effect. Then you assign the result of the second expression to the first.The problem is that the exact order in which all of those side effects are applied (assignment, postincrement, and preincrement) is unspecified; the compiler is free to reorder those operations as it sees fit. Because of this, expressions of the form
x++ = ++xwill give different results for different compilers, or for the same compiler with different compiler settings, or even based on the surrounding code.The language standard explicitly calls this out as undefined behavior so that compiler implementors are free to handle the situation any way they see fit, with no requirement to try and do the “right thing” (whatever the “right thing” may be). GCC obviously issues a diagnostic in this case, but they don’t have to. For one thing, not all cases are so easy to detect as this. Imagine a function like
Is this a problem? Only if
aandbpoint to the same thing, but if the caller is in a separate translation unit, there’s no way to know that at compile time.