By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

lvalue -modifiable and non-modifiable

P: n/a
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.

Nov 6 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Kavya" <Le******@gmail.comwrote:
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
Nov 6 '06 #2

P: n/a

Richard Bos wrote:
"Kavya" <Le******@gmail.comwrote:
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.

Nov 6 '06 #3

P: n/a
"Kavya" <Le******@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.