Lvalues and Rvalues

Discussion in 'C Programming' started by ramasubramanian.rahul@gmail.com, Oct 14, 2006.

  1. Guest

    i was reading soemthing about Lvalues Rvalues and modifiable
    Lvalues....
    i am confused now...
    like for eg
    int *y , x ;
    y = &x ;
    in the second is x a lvalue or rvalue
    any pointers on some good reading on lvalues and rvalues on the web ??
    thanks in advance
    Kind Regard
    rahul
    , Oct 14, 2006
    #1
    1. Advertising

  2. Brian C Guest

    wrote:
    > i was reading soemthing about Lvalues Rvalues and modifiable
    > Lvalues....
    > i am confused now...
    > like for eg
    > int *y , x ;
    > y = &x ;
    > in the second is x a lvalue or rvalue
    > any pointers on some good reading on lvalues and rvalues on the web ??
    > thanks in advance
    > Kind Regard
    > rahul
    >

    Well, there really isn't much to it, if I get where you're going. You
    might want to read up on http://c-faq.com/ .... this is a general C-FAQ,
    but you kind of asked a general question, so...
    Brian C, Oct 14, 2006
    #2
    1. Advertising

  3. "" <> writes:
    > i was reading soemthing about Lvalues Rvalues and modifiable
    > Lvalues....
    > i am confused now...
    > like for eg
    > int *y , x ;
    > y = &x ;
    > in the second is x a lvalue or rvalue
    > any pointers on some good reading on lvalues and rvalues on the web ??


    An "lvalue" is an expression that (potentially) designates an object.
    The origin of the term is "left value", i.e., something that can
    appear on the left side of an assignment statement (though not all
    lvalues can actually appear on the left side of an assignment
    statement).

    In your example above:
    y = &x;
    both y and x are lvalues; the operand of a unary "&" must be an
    lvalue, since "&" takes the address of an object. (It's not *quite*
    that simple, but don't worry about it.)

    A "modifiable lvalue" is an lvalue that refers to an object that can
    be modified (for example, one that isn't declared as const).

    The C standard doesn't actually use the term "rvalue". You can think
    of an rvalue as any expression that isn't used as an lvalue, or
    (loosely) as an expression that can appear on the right side of an
    assignment.

    --
    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, Oct 14, 2006
    #3
  4. SM Ryan Guest

    "" <> wrote:
    # i was reading soemthing about Lvalues Rvalues and modifiable
    # Lvalues....
    # i am confused now...
    # like for eg
    # int *y , x ;
    # y = &x ;
    # in the second is x a lvalue or rvalue
    # any pointers on some good reading on lvalues and rvalues on the web ??
    # thanks in advance

    Unfortunately except for Algol 68 and assmebly language,
    variableness is left out of type systems, requiring instead
    much verbal handwaving that could be more precise.

    Borrowing & from C++ with restriction that a reference type
    cannot be inside any other type, then when you declare a
    scalar and array,
    T v,w[n],*x;
    v has type (&T), w has type (*T), and x has type (&*T).

    Then the pointer/variable operator types are
    v = e
    v is coerceable to type &T
    and e is coerceable to type T
    and (v=e) is type T
    *x
    x is coercecable to *T
    and *x is type &T
    &v
    v is coercecable to &T
    and &v is type *T
    s.f
    If s is coercable to &T and f has nonreference type S,
    (s.f) has type &S [endowed with subnames in Algol 68].
    If s is not coercable to T and f has nonreference type S,
    (s.f) has type S.
    LOAD(v) is an implicit operator
    v is coerceable to &T
    and LOAD(v) is type T

    Expressions q in the language is then applied to one of two implicit
    functions LVALUE(q) or RVALUE(q)
    LVALUE(q) = q and q must have type &T
    RVALUE(q) = LOAD(q) if q has type &T
    RVALUE(q) = q if q has type T which is not a reference type

    The pointer/variable operator implicits are
    LVALUE(v) = RVALUE(e)
    * RVALUE(x)
    & LVALUE(x)
    s . f - There is no implicit on s.
    A variable by itself has its declared type, T or &T.
    All other expressions are
    prefixop RVALUE(operand)
    RVALUE(leftoperand) infixop RVALUE(rightoperand)
    RVALUE(predicate) ? RVALUE(truebranch) : RVALUE(falsebranch)

    GCC has an alternate rule for ?: - there is no implicit RVALUE or
    LVALUE on the branches except they must be coerceable to a balanced
    type T, T may be a reference type.
    (p?v:*x) = e
    LVALUE((RVALUE(p) ? v : *RVALUE(x))) = RVALUE(e)
    with the types LVALUE(RVALUE(int) ? &T : *RVALUE(&*T)) = RVALUE(S)
    LVALUE(RVALUE(int) ? &T : *(LOAD(&*T))) = S
    LVALUE(RVALUE(int) ? &T : *(*T)) = S
    LVALUE(RVALUE(int) ? &T : &T) = S
    LVALUE(&T) = S
    &T = S
    and if S is coerceable to T, the assignment is valid in GCC,
    but in ANSI C, the types are T=S which is invalid.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    But I do believe in this.
    SM Ryan, Oct 14, 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. Steven T. Hatton
    Replies:
    11
    Views:
    657
    Alf P. Steinbach
    Apr 20, 2004
  2. Replies:
    6
    Views:
    503
    Rolf Magnus
    Jun 8, 2005
  3. Steven T. Hatton

    cv-qualified rvalues

    Steven T. Hatton, Dec 14, 2006, in forum: C++
    Replies:
    1
    Views:
    804
    Victor Bazarov
    Dec 14, 2006
  4. jacob navia

    casts and lvalues

    jacob navia, Jun 24, 2007, in forum: C Programming
    Replies:
    68
    Views:
    1,380
    Keith Thompson
    Jun 27, 2007
  5. Nicklas Karlsson

    lvalues and rvalues

    Nicklas Karlsson, Apr 6, 2010, in forum: C Programming
    Replies:
    127
    Views:
    2,507
    Tim Rentsch
    May 5, 2010
Loading...

Share This Page