L-Value and R-Value Expressions
Related Blog Items
- Why sizeof is an operator, not a function?
- C Programming - Complicated Declaration Made Simple
- Implicit Type Conversions - part 2
- C++ Basics Tutorial - Lesson 5
- C++ Basics Tutorial - Lesson 3
Expressions that refer to memory locations are called “l-value” expressions. An l-value represents a storage region’s “locator” value,
or a “left” value, implying that it can appear on the left of the equal sign (=). L-values are often identifiers.
Expressions referring to modifiable locations are called “modifiable l-values.” A modifiable l-value cannot have an array type, an incomplete type, or a type
with the const attribute. For structures and unions to be modifiable l-values, they must not have any members with the const
attribute. The name of the identifier denotes a storage location, while the value of the variable is the value stored at that location.
An identifier is a modifiable l-value if it refers to a memory location and if its type is arithmetic, structure, union, or pointer. For
example, if ptr is a pointer to a storage region, then *ptr is a modifiable l-value that designates the storage region to which ptr points.
Any of the following C expressions can be l-value expressions:
->An identifier of integral, floating, pointer, structure, or union type
->A subscript ([ ]) expression that does not evaluate to an array
->A member-selection expression (–> or .)
->A unary-indirection (*) expression that does not refer to an array
->An l-value expression in parentheses
->A const object (a nonmodifiable l-value)
The term “r-value” is sometimes used to describe the value of an expression and to distinguish it from an l-value. All l-values are
r-values but not all r-values are l-values.
Some examples of correct and incorrect usages are:
i = 7; // Correct. A variable name, i, is an l-value.
7 = i; // Error. A constant, 7, is an r-value.
j * 4 = 7; // Error. The expression j * 4 yields an r-value.
*p = i; // Correct. A dereferenced pointer is an l-value.
const int ci = 7; // Declare a const variable.
ci = 9; // ci is a nonmodifiable l-value, so the
// assignment causes an error message to
// be generated.
((i < 3) ? i : j) = 7; // Correct. Conditional operator (? :)
// returns an l-value.
Popularity: 7%
You need to log on to convert this article into PDF
Related Blog Items - Why sizeof is an operator, not a function?
- C Programming - Complicated Declaration Made Simple
- Implicit Type Conversions - part 2
- C++ Basics Tutorial - Lesson 5
- C++ Basics Tutorial - Lesson 3
Related Blog Items
- Why sizeof is an operator, not a function?
- C Programming - Complicated Declaration Made Simple
- Implicit Type Conversions - part 2
- C++ Basics Tutorial - Lesson 5
- C++ Basics Tutorial - Lesson 3
No Comments
No comments yet.