4

in C programming
i can't understand exactly what is l-value, r-value, and casting l-value to r-value, and casting r to l.

*x = *(x+1)
1
  • Your expression is equivalent to writing x[0] = x[1] if that helps. Commented Apr 14, 2011 at 3:52

3 Answers 3

8

An rvalue is just a value -- 17, or 3.14 or something on that order.

An lvalue is (simplifying a bit) something that refers to someplace in memory that can/does hold a value.

The most common lvalue is just a variable, so in something like x=10, x is an lvalue, and 10 is an rvalue. In fact, that's the origin of the names: an lvalue was (originally) anything that could appear on the Left side of an assignment, and an Rvalue was something that could appear on the right side of an assignment.

Converting an lvalue to an rvalue basically just means retrieving the value stored in the lvalue from wherever it's stored (usually memory). There isn't really any normal conversion from an rvalue to an lvalue though -- once you have something like 10, you can't convert back to a memory location. It's just 10 at that point, and the fact that there might be some variable with the value 10 (or more than one) doesn't mean you can convert the 10 back into the variable.

Sign up to request clarification or add additional context in comments.

5 Comments

What does "simplifying a bit" means? What could be an l-value that does not refer to some place in memory?
@quickbug: there are cases where (for example) you create a static const int and basically just use it as a named constant. If you never take its address (or anything similar), chances are pretty good the compiler won't assign it an address, even though syntactically it's an lvalue.
I do not quite understand. I tried this code : static const int x = 0; const int* ptr = &x; ptr++; printf("%d", *ptr); It worked like a charm, which means that ptr is actually an l-value.
Please forget about my previous comment. This is the real one: I never considered a static const int was an l-value because it can be at the left of the equal sign only once in its life. Is this enough to qualify for l-value? I am preparing a course for undergraduate sutudents. I would not have to tell them that a static const is an l-value.
@quickbug: If it's a normal class on C, then I don't think there would be any reason to get into it. The only way I'd get into it would be if it was something like a compilers class, where I might mention this strange little corner case. If you're going to mention that, you'd probably want to mention string literals as well: another "thing" that's officially an lvalue, but if you ever assign to its contents, you get undefined behavior.
2

The C standard recognizes the original terms placed for left and right as in L = R; however, it says to think of lvalue as locator value, which roughly means you can get the address of an object and therefore that object has a location.

The r-value is said to as a The Value of an expression which may be literals , int ,float any value.

So in you example left hand side is a Location value in which the right hand expression value is going to be set.

Comments

0

How about the explanation here? http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Flvalue.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.