gpt4 book ai didi

c - 无序修改和访问指针

转载 作者:太空狗 更新时间:2023-10-29 17:00:37 26 4
gpt4 key购买 nike

我收到此 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的修改不在or之前排序在表达式右侧使用 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

关于c - 无序修改和访问指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32017561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com