Question

int x = 3;
int y = 5;
x++ = y;
cout << x << endl;

Why is the above code giving me an error, the specific error I get is

lvalue required as left operand of assignment.

I am trying to review up on my C and C++. From my understanding the above code should theoretically copy over the value of y into x, then finally increment the value of x after the copy has finished. So it would print out "6".

Was it helpful?

Solution 8

x++ results in a temporary object that has x value before the increment. And you cannot assign a temp object.

Examples for clarification:

int a=1; 
a=5; // Lvalue = Rvalue, Ok 
a=a; // Lvalue = Lvalue, Ok 
5=a; // Rvalue = Lvalue Comp. error
5=5; // Rvalue = Rvalue Comp. error 

OTHER TIPS

x++ ==> x=x+1

x++ = y ==> (x+1)=y

now x+1=y will throw lvalue required which means left hand side of = should be a variable not a constant

The result of the postincrement operator is a temporary object that has the value before incrementing. You may not assign a temporary object. So this statement is invalid.

x++ = y;

You could rewrite it the following way

( x += 1 ) = y;

The statement above is correct.

x++ cannot be use as left operand because it returns a value, not an address.

In this line:

x++ = y;

x++ is r-value, and you cannot assign to it, you can rewrite it as follows: 3 = 5; which makes no sense, you cannot assign 5 to 3.

In your expression: x++ = y, x++ is just a result of an evaluated expression so you can't assigne a value to a result.

Another think is that this expression has no meaning since the variable x will be updated with the value of y.

The result of x++ is the value of x, but not the object x. The expression is a prvalue, which means you can't then assign to it (you need an lvalue).

The value of a postfix ++ expression is the value of its operand. [ Note: the value obtained is a copy of the original value — end note ] ... The result is a prvalue.

To achieve what you want, you need to do:

x = y;
x++;

In C++11, with its new sequencing rules, you could also do:

(x = y)++;

This would be undefined behaviour in C++03 because there is no sequence point between two modifications of x.

From my understanding the above code should theoretically copy over the value of y into x, then finally increment the value of x after the copy has finished.

x++ evaluates to an r-value (constant). Assignment operator = needs an l-value (variable) as its left operand.

x++ = y ==> (3 + 1) = 5 ==> 4 = 5  

you can't assign a constant to a constant. x++ = y is not a valid statement in either of C or C++.

The core issue is this:

From my understanding the above code should theoretically copy over the value of y into x, then finally increment the value of x after the copy has finished. So it would print out "6".

In fact, the sequencing of the increment is undefined. As in, it is completely undefined as to when x is incremented, except that it must occur before some specific times. Reading or writing x during this period of undefined value is undefined behaviour. That's why x++ does not return x (an lvalue you can write to) but an rvalue you cannot write to- because there's simply nothing useful you can do with x until the time it's guaranteed to be incremented (except take the address, maybe).

You can still break this rule by referring to x twice- for example, consider:

void f(int, int);
int main() {
    int x = 0;
    f(x++, x++);
}

This is also undefined behaviour.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top