lvalue vs rvalue

In reading a book I picked up at the library, "C++ Pointers and Memory Management", Daconta, I came across a section discussing the topic.

I thought I knew what lvalue and rvalue meant before. Simply a left side of assignment operator, and right side of assignment operator. I never knew that lvalue was actually the memory address and rvalue was the contents of the memory address. So something like:

1
2
3
4
int c = 12;
int b = 6;

c = b; //c is the lvalue in this situation, meaning it yields a memory location, while b is the rvalue, and yields the contents of a memory address (ie &b) 


This sounds weird to me. Is this true? If so, can someone explain this a little differently. The section in this book about is about a page and a half and this all I've gathered from it
I thought the same as you before as well. Are you sure the book wasn't just talking about the rvalue in a certain example being a memory address (pointer) rather than all rvalues? Of course everything works by memory address in the final assembly, but this isn't that deep. Technically memory addresses are just different types of values.
Yup I'm sure he meant this statement as all rvalues. It kind of makes sense

I'm paraphrasing here...
In an assignment, the left operand yields a memory address for the right operand to put a value into. The rvalue is yielding the value it holds.


This makes sense, but the way I understood it before is that a variable can be an lvalue or rvalue. This author is saying that each variable has both an rvalue and lvalue. Do you see the difference here? Sounds weird to me, and I don't know why. Maybe it's cause I always thought of it differently.
Yea I noticed when I grabbed the book it looked old, so I checked the publish date. Does seem rather dated, and I can tell by the author's style. But, for the most part he seems to get the points across pretty well. And it seems to be a topic most I've seen shy away from.

((i < 3) ? i : j) = 7; This seems interesting that's valid. Doesn't this just return a true or false? Not sure how the ternary operator returns a lvalue.

And looks as if there 3 are more value types in C++11. Fun fun. I guess I should read up on those as well, though from the SO link they don't look too crazy.

EDIT:
Nevermind on the ternary operator, had to go read the docs on it again. I was thinking of it weird in my head. I never use it.
Last edited on
I wrote up a little on this topic at http://en.cppreference.com/w/cpp/language/value_category

The important thing to remember that lvalue/rvalue are kinds of expressions, not variables.


To answer the original question,
c = b; //c is the lvalue in this situation, meaning it yields a memory location, while b is the rvalue, and yields the contents of a memory address

"c" is an lvalue subexpression and "b" is an lvalue subexpression.
b undergoes lvalue-to-rvalue implicit conversion, which is very similar to the infamous array-to-pointer implicit conversion.. so if the author says 'b is the rvalue', it's the same as saying 'array is a pointer'
Last edited on
Topic archived. No new replies allowed.