lvalue -modifiable and non-modifiable

Discussion in 'C Programming' started by Kavya, Nov 6, 2006.

  1. Kavya

    Kavya Guest

    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.
    Kavya, Nov 6, 2006
    #1
    1. Advertising

  2. Kavya

    Richard Bos Guest

    "Kavya" <> wrote:

    > 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
    Richard Bos, Nov 6, 2006
    #2
    1. Advertising

  3. Kavya

    Kavya Guest

    Richard Bos wrote:
    > "Kavya" <> wrote:
    >
    > > 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.
    Kavya, Nov 6, 2006
    #3
  4. "Kavya" <> writes:
    > 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) <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.
    Keith Thompson, Nov 6, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Biber

    Re: Why is this not a modifiable lvalue.

    Simon Biber, Jun 27, 2003, in forum: C Programming
    Replies:
    0
    Views:
    2,341
    Simon Biber
    Jun 27, 2003
  2. Dan Pop

    Re: Why is this not a modifiable lvalue.

    Dan Pop, Jun 27, 2003, in forum: C Programming
    Replies:
    6
    Views:
    4,886
    Dan Pop
    Jun 30, 2003
  3. Martin Ambuhl

    Re: Why is this not a modifiable lvalue.

    Martin Ambuhl, Jun 27, 2003, in forum: C Programming
    Replies:
    0
    Views:
    692
    Martin Ambuhl
    Jun 27, 2003
  4. Micah Cowan

    Re: Why is this not a modifiable lvalue.

    Micah Cowan, Jun 28, 2003, in forum: C Programming
    Replies:
    1
    Views:
    746
    CBFalconer
    Jun 28, 2003
  5. v4vijayakumar

    variable - modifiable lvalue

    v4vijayakumar, May 15, 2006, in forum: C Programming
    Replies:
    1
    Views:
    345
Loading...

Share This Page