첫째, 일부 standardese :
6.5.2.4 Postfix increment and decrement operators
Constraints
1 The operand of the postfix increment or decrement operator shall have atomic, qualified, or unqualified real or pointer type,
and shall be a modifiable lvalue.
Semantics
2 The result of the postfix
++
operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).
See the discussions of additive operators and compound assignment for information on constraints, types, and conversions and the effects of operations on pointers. The value computation of the result is sequenced before the side effect of updating the stored value of the operand. With respect to an indeterminately-sequenced function call, the operation of postfix
++
is a single evaluation. Postfix
++
on an object with atomic type is a read-modify-write operation with
memory_order_seq_cst
memory order semantics.
98)
...
6.5.16 Assignment operators
...
3 An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment,
111)
but is not an lvalue. The type of an assignment expression is the type the left operand would have after lvalue conversion. The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands. The evaluations of the operands are unsequenced.
강조 광산.
위 벽의 결과는 ptr++
과 ++ptr
의 결과가 lvalues가 아님을 나타냅니다. 그러나 두 표현식 모두 포인터 값을 가지므로 단항 *
연산자의 피연산자 일 수 있으며 *ptr++
및 *++ptr
의 결과는 일 수 있습니다.
이것은 ++*++ptr
가 작동하는 이유입니다. 당신은 *++ptr
의 결과를 증가시키고 있습니다. 이것은 lvalue 일 수 있습니다.그러나 *ptr++++
은 *(((ptr)++)++)
으로 해석됩니다 (접미어 ++
은 단항 문자보다 높은 우선 순위를가집니다) *
; ptr++
의 결과는 두 번째 ++
에 대한 피연산자이지만 ptr++
의 결과는 lvalue가 아니므로 컴파일러는 불평합니다. (*ptr++)++
으로 작성한 경우 표현식이 유효합니다. 한마디로
:
++*++ptr - valid, equivalent to ++(*ptr++)
*++++ptr - invalid, equivalent to *(++(++ptr)), result of ++ptr is not an lvalue
++++*ptr - invalid, equivalent to ++(++(*ptr)), result of ++*ptr is not an lvalue
*ptr++++ - invalid, equivalent to *((ptr++)++), result pf ptr++ is not an lvalue
(*ptr)++++ - invalid, equivalent to ((*ptr)++)++, result of (*ptr)++ is not an lvalue
(*ptr++)++ - valid
Gopi 나는 두 번째 문이 나에게 오류를 제공하는 이유, 내가 설명이 필요 이해 : 좌변이 증가 피연산자로 필요한 의 printf를 ("% d 개", * PTR을 ++++); – Sagrian