无序修改和访问指针

unsequenced modification and access to pointer

我收到此 C 表达式的警告:

*p0++ = mult(*p0, psign[i1]); 

警告是:

unsequenced modification and access to 'p0' [-Wunsequenced]

我认为表达式应该修改为:

*p0 = mult(*p0, psign[i1]);
p0++;

行为(修改后)是否符合预期?我认为指针增量应该发生在 p0 指向的值更新之后。

您在上面提供的代码段调用了未定义的行为。根据C标准

C11:6.5 表达式:

If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84).

在表达式 *p0++ = mult(*p0, psign[i1]) 中,对赋值运算符左侧 p0 的修改在表达式右侧使用 p0 之前或之后没有排序.因此,片段

*p0++ = mult(*p0, psign[i1]);   

不等同于

*p0 = mult(*p0, psign[i1]);
p0++;                       // Side effect to p0 is guaranteed after the use  
                            // of p0 in mult function