gpt4 book ai didi

c - (*&) 是左值还是 r 值?

转载 作者:太空宇宙 更新时间:2023-11-04 02:21:21 24 4
gpt4 key购买 nike

首先,我认为它是一个右值,但下面的事实改变了我的想法。

我尝试了一个表达式 &(*&a) 并且它工作正常,但是运算符 & 只能使用 lvalue,所以 (*&a) 是一个 lvalue,为什么?

最佳答案

根据 C 2018 6.5.3.2 4(讨论 unary * 运算符),一元 * 的结果是一个左值:

… If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object.…

这告诉我们 *&a 是一个左值。但是题中问到的表达式是(*&a),所以要考虑括号的作用。

6.3.2.1 2(讨论自动转换)似乎告诉我们 (*&a) 被转换为 *&a 中的值而不是左值:

Except when it is the operand of the sizeof operator, the unary & operator, the ++ operator, the -- operator, or the left operand of the . operator or an assignment operator, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue); this is called lvalue conversion.

但是,6.5.1 5(讨论带括号的表达式)与此矛盾:

A parenthesized expression is a primary expression. Its type and value are identical to those of the unparenthesized expression. It is an lvalue, a function designator, or a void expression if the unparenthesized expression is, respectively, an lvalue, a function designator, or a void expression.

这是C标准的缺陷; 6.5.1 5 和 6.3.2.1 2 相互矛盾。留给我们理解的是 6.5.1 5,它专门关于括号表达式,优先于更一般的 6.3.2.1 2,这就是所有 C 实现的行为方式。

因此 (*&a) 是一个左值。

关于c - (*&) 是左值还是 r 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57691619/

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