vague lvalue vs rvalue question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chad

    vague lvalue vs rvalue question

    The following question actually stems from an old Chris Torek post.

    And I quote from the following old CLC url



    "Mathematic ally speaking, unary `&' and `*' are inverse functions.
    Unary `&' takes an lvalue of type `T' and produces an rvalue of
    type `pointer to T'; unary `*' takes an rvalue of type `pointer to
    T' and produces an lvalue of type `T'. The <value, typepairs
    from unary `&' must be distinct for distinct lvalues[*], and there
    may be only one value produced for any particular lvalue. This
    makes it an invertible function; `*' is then defined as the inverse
    function. While `*' is a well-behaved function on conventional
    architectures, all that the language requires is that it be the
    inverse of `&'. "

    I don't see how one is the inverse of the other. Can someone provide a
    concrete example?


    Chad


  • Jack Klein

    #2
    Re: vague lvalue vs rvalue question

    On Tue, 15 Apr 2008 18:23:15 -0700 (PDT), Chad <cdalten@gmail. com>
    wrote in comp.lang.c:
    The following question actually stems from an old Chris Torek post.
    >
    And I quote from the following old CLC url
    >

    >
    "Mathematic ally speaking, unary `&' and `*' are inverse functions.
    Unary `&' takes an lvalue of type `T' and produces an rvalue of
    type `pointer to T'; unary `*' takes an rvalue of type `pointer to
    T' and produces an lvalue of type `T'. The <value, typepairs
    from unary `&' must be distinct for distinct lvalues[*], and there
    may be only one value produced for any particular lvalue. This
    makes it an invertible function; `*' is then defined as the inverse
    function. While `*' is a well-behaved function on conventional
    architectures, all that the language requires is that it be the
    inverse of `&'. "
    >
    I don't see how one is the inverse of the other. Can someone provide a
    concrete example?
    Given an lvalue L of type T, the & operator produces something called
    "the address of L", which has the type "pointer to type T".

    Given a pointer to type T, the * operator access the lvalue of type T
    that the pointer points to (assuming, of course, that it actually is
    initialized to point to a type T object).

    How about a concrete example:

    #include <stdio.h>

    int main(void)
    {
    int i = 3;
    int *ip = &i;
    double d = 1.5;
    double *dp = &d;

    printf("i = %d and *ip = %d\n", i, *ip);
    printf("d = %f and *dp = %f\n", d, *dp);

    return 0;
    }

    Do you understand why the output is:

    i = 3 and *ip = 3
    d = 1.500000 and *dp = 1.500000

    I hope so. Notice that the second value is each output line is taken
    by applying the * operator to a value that was obtained by the &
    operator.

    But we can make it more explicit by replacing the two printf() calls
    in the program by these:

    printf("i = %d and *ip = %d\n", i, *&i);
    printf("d = %f and *dp = %f\n", d, *&d);

    This might get you a warning message from your compiler that you have
    assigned values to "ip" and "dp" that are never used, but when you
    build and run the program you will get exactly the same output.

    You are taking the address of 'i' or 'd' with &, then immediately
    using '*' to dereference that address and retrieve its contents. You
    are just doing it directly, without storing the addresses in
    intermediate pointer objects.

    So applying & to an lvalue produces an address rvalue, and applying *
    to that rvalue produces the lvalue.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++

    Comment

    • Eric Sosman

      #3
      Re: vague lvalue vs rvalue question

      Chad wrote:
      The following question actually stems from an old Chris Torek post.
      >
      And I quote from the following old CLC url
      >

      >
      "Mathematic ally speaking, unary `&' and `*' are inverse functions.
      Unary `&' takes an lvalue of type `T' and produces an rvalue of
      type `pointer to T'; unary `*' takes an rvalue of type `pointer to
      T' and produces an lvalue of type `T'. The <value, typepairs
      from unary `&' must be distinct for distinct lvalues[*], and there
      may be only one value produced for any particular lvalue. This
      makes it an invertible function; `*' is then defined as the inverse
      function. While `*' is a well-behaved function on conventional
      architectures, all that the language requires is that it be the
      inverse of `&'. "
      >
      I don't see how one is the inverse of the other. Can someone provide a
      concrete example?
      int thing = 42;
      int *ptr = &thing;
      assert(*&thing == thing);
      assert (&*ptr == ptr);

      --
      Eric Sosman
      esosman@ieee-dot-org.invalid

      Comment

      Working...