lvalue -modifiable and non-modifiable

K

Kavya

Can someone give and explain in simple terms a definition of lvalue?
Also what are these modifiable and non-modifiable lvalues?
I always thought that, if we can assign to anything then that anything
is lvalue and if cannot assign to anything then that anything is not
lvalue.
 
R

Richard Bos

Kavya said:
Can someone give and explain in simple terms a definition of lvalue?

Sure. The Standard can, in [6.3.2.1#1]:

# An lvalue is an expression with an object type or an incomplete type
# other than void;
Also what are these modifiable and non-modifiable lvalues?

From the same bit of the Standard:

# A modifiable lvalue is an lvalue that does not have array type, does
# not have an incomplete type, does not have a const-qualified type, and
# if it is a structure or union, does not have any member (including,
# recursively, any member or element of all contained aggregates or
# unions) with a const-qualified type.
I always thought that, if we can assign to anything then that anything
is lvalue and if cannot assign to anything then that anything is not
lvalue.

That's a common use of the term, but not as it's used in the ISO C
Standard. It's questionable if this was a good idea, but that's what we
have to work with.

Richard
 
K

Kavya

Richard said:
Kavya said:
Can someone give and explain in simple terms a definition of lvalue?

Sure. The Standard can, in [6.3.2.1#1]:

# An lvalue is an expression with an object type or an incomplete type
# other than void;
Also what are these modifiable and non-modifiable lvalues?

From the same bit of the Standard:

# A modifiable lvalue is an lvalue that does not have array type, does
# not have an incomplete type, does not have a const-qualified type, and
# if it is a structure or union, does not have any member (including,
# recursively, any member or element of all contained aggregates or
# unions) with a const-qualified type.
I always thought that, if we can assign to anything then that anything
is lvalue and if cannot assign to anything then that anything is not
lvalue.

That's a common use of the term, but not as it's used in the ISO C
Standard. It's questionable if this was a good idea, but that's what we
have to work with.


Thank You Very Much.
 
K

Keith Thompson

Kavya said:
Can someone give and explain in simple terms a definition of lvalue?
Also what are these modifiable and non-modifiable lvalues?
I always thought that, if we can assign to anything then that anything
is lvalue and if cannot assign to anything then that anything is not
lvalue.

Briefly, an lvalue is an expression that designates an object.
Unfortunately, it's not quite that simple, and both C90 and C99 have
seriously flawed definitions of "lvalue".

Here's the definition from the C90 standard, C90 6.2.2.1p1:

An _lvalue_ is an expression (with an object type or an incomplete
type other than void) that designates an object. When an object is
said to have a particular type, the type is specified by the
lvalue used to designate the object. A _modifiable lvalue_ is an
lvalue that does not have array type, does not have an incomplete
type, does not have a const-qualified type, and if it is a
structure or union, does not have any member (including,
recursively, any member of all contained structures or unions)
with a const-qualified type.

with a footnote similar to the one in the C99 standard (see below).

The C90 standard's definition had a fairly serious problem. If the
above definition is taken literally, then we can't necessarily
determine whether a given expression is an lvalue until we know its
value at execution time. This is clearly unacceptable; the compiler
needs to know *during compilation* whether an expression in certain
contexts is an lvalue or not. It also clearly was not the intent.
C90's definition failed to capture the idea that if p is a pointer, *p
doesn't cease to be an lvalue if p == NULL.

Here's the definition from the C99 standard, C99 6.3.2.1p1:
An _lvalue_ is an expression with an object type or an incomplete
type other than void; if an lvalue does not designate an object
when it is evaluated, the behavior is undefined. When an object
is said to have a particular type, the type is specified by the
lvalue used to designate the object. A _modifiable lvalue_ is an
lvalue that does not have array type, does not have an incomplete
type, does not have a const-qualified type, and if it is a
structure or union, does not have any member (including,
recursively, any member or element of all contained aggregates or
unions) with a const-qualified type.

with a footnote:
The name "lvalue" comes originally from the assignment expression
E1 = E2, in which the left operand E1 is required to be a
(modifiable) lvalue. It is perhaps better considered as
representing an object "locator value". What is sometimes called
"rvalue" is in this International Standard described as the "value
of an expression".

The C99 standard attempted to correct this flaw by dropping the
wording that an lvalue must designate an object. But designating an
object is the whole idea of what an lvalue is. The cure was worse
than the disease.

Reading the C99 definition literally, the integer constant 42 is an
lvalue, since it's an expression with an object type (namely int), and
evaluating that expression *in any context* invokes undefined behavior
since it does not designate an object.

Clearly this was not the intent either.

I don't believe it's impossible to interpret either definition
literally to yield a consistent description of the language.

An lvalue is an expression that *potentially* designates an object.
It may do so unconditionally (for example, an object name is always an
lvalue), or it may do so conditionally (for example, if p is a
pointer, then *p is an lvalue regardless of whether it *currently*
designates an object or not). 42 is not an lvalue because it cannot
ever designate an object. If an lvalue does not designate an object
when it's evaluated, the behvior is undefined.

This has been discussed at length in comp.std.c.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top