Why is ++i
considered lvalue and i++
rvalue?
I found the answer to this question on stackoverflow , but my terrible English does not allow me to properly understand this. After all, the priority of prefix and postfix ++
is still higher than &
and in theory, in any case, it will be ++
at first, but only then &
or do I not understand that at all?
6 answers
Because after the execution of expressions:
i = 0; (1) x = ++i; (2) x = i++; В x будут следующие значения: (1) x = 1; i = 1; (2) x = 0; i = 1;
All effects are associated with just such a division.
That is, ++i
means to increase i
by one and take it for an expression, and i++
means to take the value of i
for an expression and then to increase i
by 1
.
- IonRod you confuse the concepts of prefix / postfix with the semantics of an assignment, and these are two different things! - perfect
- @perfect like not? - Qwertiy ♦
Perhaps this is a very incomplete answer compared to the first one deployed by reference, but the point is this:
The postfix operator changes the value and returns only a temporary copy of this value, which, as a result, cannot be changed. That is, this copy goes separately from the i
value itself, and can be used in the expression, but assignment to i++
does not make sense, because the result of i++
stored in some other memory cell, not in the one where i
lies.
The result of ++i
written to it, so the expression ++i = ...
makes sense (l-value).
- oneIn fact,
++i
should return a value, and++i =
makes no sense to have <br> PS - the operator changes the variable, and returns a value - timka_s - 2In C ++
++i = x;
compiles and works. Returns, but not to a temporary copy. - ivkremer - ++ i works just by reference to a variable, so it runs faster than i ++, which returns values. - Stas Litvinenko
- one> x = x ++; // the variable does not change at all. In my opinion it depends on the compiler. May change, and maybe not. - gammaker
- oneThanks, just checked, in C ++ (g ++) it really changes. In Java, it just never changes, I deleted it from the answer. - ivkremer
Live and learn. I took and tried the options discussed.
#include <stdio.h> #include <stdlib.h> main () { int i = 0, x; x = (++i + ++i); printf ("i = 0; x = (++i + ++i): x=%di=%d\n",x,i); i = 0; x = (i++ + i++); printf ("i = 0; x = (i++ + i++): x=%di=%d\n",x,i); x = x++; printf ("x = x++: x=%di=%d\n",x,i); #ifdef __cplusplus ++i = x; printf ("++i = x: x=%di=%d\n",x,i); #endif } c:/Users/avp/src/cc/tst $ gcc tc c:/Users/avp/src/cc/tst $ ./a i = 0; x = (++i + ++i): x=4 i=2 i = 0; x = (i++ + i++): x=0 i=2 x = x++: x=0 i=2 c:/Users/avp/src/cc/tst $ g++ tc c:/Users/avp/src/cc/tst $ ./a i = 0; x = (++i + ++i): x=4 i=2 i = 0; x = (i++ + i++): x=0 i=2 x = x++: x=0 i=2 ++i = x: x=0 i=0 c:/Users/avp/src/cc/tst $ g++ --version g++.exe (GCC) 3.4.5 (mingw-vista special r3) Copyright (C) 2004 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. c:/Users/avp/src/cc/tst $ gcc --version gcc.exe (GCC) 3.4.5 (mingw-vista special r3) Copyright (C) 2004 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. c:/Users/avp/src/cc/tst $ c:/Users/avp/src/cc/tst $
- I'm sorry I ask, but did you use the shell code for windows? - sudo97
The i++
operator cannot return an lvalue for any reason, for the simple reason that there is no suitable lvalue to return anywhere.
The postfix operator ++
must return the old value of its operand. However, when the side effects of postfix ++
had their effect, its operand has already changed its value to the new, and the old values of the operand are not stored anywhere else in memory. Those. the returned lvalue simply has nowhere to "bind" in memory. The only way to return the old value is either to memorize this old value in a temporary object, or to calculate it on the fly from the new value. In both cases, the old value will not be lvalue.
The situation with the prefix operator is the reverse. The prefix operator must return the new value of its operand. It is this new value that the operand will contain after implementing the side effects of the prefix ++
. Therefore, the operand of the prefix operator is precisely the lvalue that the operator will immediately return.
Why is ++ i considered lvalue and i ++ rvalue?
Because these operators are so declared in accordance with the standard:
- Pre-increment returns a reference . A link can be assigned a value, and therefore it is an lvalue ("left side value", the value to the left of the equal sign).
- Post-increment returns value . The value can not be assigned; he himself is assigned, and therefore it is rvalue ("right side value", the value to the right of the equal sign).
i++; /*называется постфиксной записью, в которой приоритет операции низкий*/ ++i; /*называется префиксной записью, в которой приоритет операции высокий*/ /*----------------------------------------------------------------------------------*/ int i = 5; int x = i++; /*здесь x == 5, а i == 6*/ int x = ++i; /*здесь x == 6, i == 6*/
- fiveAnd what's the point of this answer?) - timka_s