[英]The behavior of unary & operator when met []
I've been reading C11 6.5.3.2 p3我一直在阅读 C11 6.5.3.2 p3
The unary & operator yields the address of its operand.一元 & 运算符产生其操作数的地址。 If the operand has type “type”, the result has type “pointer to type”.如果操作数的类型为“type”,则结果的类型为“指向类型的指针”。 If the operand is the result of a unary * operator, neither that operator nor the & operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue.如果操作数是一元 * 运算符的结果,则该运算符和 & 运算符都不会被计算,结果就像两者都被省略了一样,除了对运算符的约束仍然适用并且结果不是左值。 Similarly, if the operand is the result of a [] operator, neither the & operator nor the unary * that is implied by the [] is evaluated and the result is as if the & operator were removed and the [] operator were changed to a+ operator.类似地,如果操作数是 [] 运算符的结果,则 & 运算符和 [] 隐含的一元 * 都不会计算,结果就好像 & 运算符已被删除并且 [] 运算符已更改为a+ 运算符。 Otherwise, the result is a pointer to the object or function designated by its operand.否则,结果是指向由其操作数指定的对象或函数的指针。
I can't understand most of this paragraph although reading it repeatedly.尽管反复阅读,我还是无法理解这一段的大部分内容。
My problem parts are * that is implied by the []
, []operator were changed to a + operator
, function designated by this operand
.我的问题部分是* that is implied by the []
, []operator were changed to a + operator
, function designated by this operand
。 This paragraph is talking about & but why does * appear and term "function designated" appear after saying [].这一段讲的是&,但是为什么会出现*,并且在说[] 之后会出现术语“指定的函数”。 And [] operator were changed to a + operator
seems trying to say the definition of array : E1[E2] = *((E1) + (E2))
What does these lines mean?而[] operator were changed to a + operator
似乎在试图说出数组的定义: E1[E2] = *((E1) + (E2))
这些行是什么意思? I need some help.我需要帮助。
If the operand is the result of a unary * operator, neither that operator nor the & operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue.如果操作数是一元 * 运算符的结果,则该运算符和 & 运算符都不会被计算,结果就像两者都被省略了一样,除了对运算符的约束仍然适用并且结果不是左值。
Unary &
applied to the result of unary *
, cancels the *
and converts the operand of the original *
to an r-value:一元&
应用于一元*
的结果,取消*
并将原始*
的操作数转换为 r 值:
#include <assert.h>
int main()
{
int *p=&(int){42};
assert(&*p == p); //the value (42) is not fetched from the target
#if 0
&*p = &(int){1000}; //not OK; & cancels the * but converts p to an r-value (can't be on the left-hand side of an assignment)
#endif
p = &(int){1000}; //ok; p is an l-value (can be on the left hand side of an assignment)
//(more accurately: can have its address taken)
}
Now since a pointerOrArray[index]
expression is defined ( 6.5.2.1p2 ) as *(pointerOrArray+index)
(the result of a unary *
, except the *
is hidden), you can apply the same rule to it: &pointerOrArray[index]
<=> (pointerOrArry+Index)
.现在,由于pointerOrArray[index]
表达式被定义为 ( 6.5.2.1p2 ) *(pointerOrArray+index)
(一元*
的结果,除了*
是隐藏的),您可以对其应用相同的规则: &pointerOrArray[index]
<=> (pointerOrArry+Index)
。 That's what your first quoted sentence says.这就是你引用的第一个句子所说的。
The last sentence you quoted can be (within the context of 6.5.3.2p3 ) interpreted as:您引用的最后一句话可以(在6.5.3.2p3的上下文中)解释为:
Otherwise (if unary &
isn't combined with *
or []
), the result (of unary &
) is a pointer to the object ( &object
) or function ( &function
) designated by its operand (an object
or a function
).否则(如果一元&
不与组合*
或[]
则结果(一元&
)是一个指针,指向所述对象( &object
)或功能( &function
)由它的操作数(一个指定的object
或function
)。
[From a comment] I can't sure
[]
's meaning. [来自评论] 我不确定[]
的意思。 It's not written asarray[i]
just[]
.它不写为array[i]
只是[]
。
The C standard uses []
to mean the subscript operator. C 标准使用[]
表示下标运算符。 Although it appears in source code as E1[E2]
, where E1
and E2
stand for expressions, the [
and ]
characters are the fundamental way it is recognized, and they designate an operation of array subscripting.尽管它在源代码中显示为E1[E2]
,其中E1
和E2
代表表达式,但[
和]
字符是识别它的基本方式,它们指定了数组下标操作。
[C 2011, draft N1570, 6.5.3.2 3, discussing the unary
&
operator] … Similarly, if the operand is the result of a[]
operator, neither the&
operator nor the unary*
that is implied by the[]
is evaluated and the result is as if the&
operator were removed and the[]
operator were changed to a+
operator. [C 2011,草案 N1570,6.5.3.2 3,讨论一元&
运算符] ...同样,如果操作数是[]
运算符的结果,则&
运算符和[]
隐含的一元*
不会被计算结果就好像&
运算符被删除并且[]
运算符被更改为+
运算符。 Otherwise, the result is a pointer to the object or function designated by its operand.否则,结果是指向由其操作数指定的对象或函数的指针。
Going back to the []
operator, consider an expression E1[E2]
.回到[]
运算符,考虑表达式E1[E2]
。 The definition of the subscript operator, in 6.5.2.1 2, is that it is identical to (*((E1)+(E2)))
. 6.5.2.1 2 中下标运算符的定义是它与(*((E1)+(E2)))
。 (In other words, it takes the pointer E1
and adds the subscript E2
to it, then applies *
to get the element at that location. Or, since +
is commutative, E2
can be the pointer, and E1
can be the subscript.) So “the unary *
that is implied by the []
” is that *
in (*((E1)+(E2)))
. (换句话说,它获取指针E1
并为其添加下标E2
,然后应用*
以获取该位置的元素。或者,由于+
是可交换的,因此E2
可以是指针,而E1
可以是下标。)因此,“一元*
由隐含[]
是” *
在(*((E1)+(E2)))
So, when we have &
applied to E1[E2]
, this passage tells us to consider it as &(*((E1)+(E2)))
and that neither the &
nor the *
are evaluated, so it is as if it were ((E1)+(E2))
.因此,当我们将&
应用于E1[E2]
,这段话告诉我们将其视为&(*((E1)+(E2)))
并且既不计算&
也不计算*
,因此就好像它是((E1)+(E2))
。
… why does … [the] term "function designated" appear… ...... 为什么...... [the] 术语“指定的功能”出现......
The sentence containing “function designated” is separate.包含“功能指定”的句子是分开的。 The “Otherwise” tells us it is talking about the situation other than when &
is applied to a subscript expression (from the sentence before) or to a unary *
expression (from two sentences before). “Otherwise”告诉我们它是在讨论除&
应用于下标表达式(来自之前的句子)或一元*
表达式(来自之前的两个句子)之外的情况。 So we just have &E
, where E
is something other than *E1
or E1[E2]
.所以我们只有&E
,其中E
不是*E1
或E1[E2]
。 This sentence says “Otherwise, the result is a pointer to the object or function designated by its operand.”这句话说“否则,结果是指向由其操作数指定的对象或函数的指针。” If E
is an object, then &E
is a pointer to the object.如果E
是一个对象,则&E
是指向该对象的指针。 If E
is a function, then &E
is a pointer to the function.如果E
是函数,则&E
是指向该函数的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.