Pointer-reference question

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

    Pointer-reference question

    Can someone please explain this to me... if I have:

    int x = 10;

    I can do:
    int* p_x = &x;

    Compiles okay.

    If I have a function though:
    int ReturnInt() { return x; }
    // Where x = 10;

    and then do:

    int* p_x = &ReturnInt() ;

    I get a compiler error "C2102 '&' requires l-value".

    I had thought that these should be the same. Clearly they are not, but Im
    not sure why.

    Thanks,
    B


  • Gianni Mariani

    #2
    Re: Pointer-reference question

    BCC wrote:[color=blue]
    > Can someone please explain this to me... if I have:
    >
    > int x = 10;
    >
    > I can do:
    > int* p_x = &x;
    >
    > Compiles okay.
    >
    > If I have a function though:
    > int ReturnInt() { return x; }
    > // Where x = 10;
    >
    > and then do:
    >
    > int* p_x = &ReturnInt() ;
    >
    > I get a compiler error "C2102 '&' requires l-value".
    >
    > I had thought that these should be the same. Clearly they are not, but Im
    > not sure why.[/color]

    You're taking the address of a return value ?

    Take this for example:

    int * p = &(5+5);

    In this case, 10 is not stored in memory, it's the result of an
    expression and never is stored in the address space of the program so
    you can't take the address of it. Also the return value from a function
    is also not allways addressible as a pointer.

    Anyhow, the issue really is that the DEFINITION OF THE LANGUAGE does not
    permit you to take the address of a non "l-value" which is defined in
    the language.

    An l-value (left hand side value) is an expression that refers to an
    object (or pod).


    Comment

    • Thomas Wintschel

      #3
      Re: Pointer-reference question


      "BCC" <a@b.c> wrote in message
      news:0MQtb.3182 9$2P7.57@newssv r27.news.prodig y.com...[color=blue]
      > Can someone please explain this to me... if I have:
      >
      > int x = 10;
      >
      > I can do:
      > int* p_x = &x;
      >
      > Compiles okay.
      >
      > If I have a function though:
      > int ReturnInt() { return x; }
      > // Where x = 10;
      >
      > and then do:
      >
      > int* p_x = &ReturnInt() ;
      >
      > I get a compiler error "C2102 '&' requires l-value".
      >
      > I had thought that these should be the same. Clearly they are not, but Im
      > not sure why.
      >
      > Thanks,
      > B
      >
      >[/color]

      The return value of ReturnInt() is a temporary object. You can assign it to
      something, use it to initialize something etc. but you cannot safely take
      it's
      address since it will cease to exist once the current line of code is
      executed.

      Tom


      Comment

      • David White

        #4
        Re: Pointer-reference question

        "BCC" <a@b.c> wrote in message
        news:0MQtb.3182 9$2P7.57@newssv r27.news.prodig y.com...[color=blue]
        > Can someone please explain this to me... if I have:
        >
        > int x = 10;
        >
        > I can do:
        > int* p_x = &x;
        >
        > Compiles okay.
        >
        > If I have a function though:
        > int ReturnInt() { return x; }
        > // Where x = 10;
        >
        > and then do:
        >
        > int* p_x = &ReturnInt() ;
        >
        > I get a compiler error "C2102 '&' requires l-value".
        >
        > I had thought that these should be the same. Clearly they are not, but Im
        > not sure why.[/color]

        Others have already answered. I just wanted to point out that your question
        is entirely about addresses and pointers, and is completely unrelated to
        references.

        DW



        Comment

        • Gianni Mariani

          #5
          Re: Pointer-reference question

          David White wrote:[color=blue]
          > "BCC" <a@b.c> wrote in message
          > news:0MQtb.3182 9$2P7.57@newssv r27.news.prodig y.com...
          >[color=green]
          >>Can someone please explain this to me... if I have:
          >>
          >>int x = 10;
          >>
          >>I can do:
          >>int* p_x = &x;
          >>
          >>Compiles okay.
          >>
          >>If I have a function though:
          >>int ReturnInt() { return x; }
          >>// Where x = 10;
          >>
          >>and then do:
          >>
          >>int* p_x = &ReturnInt() ;
          >>
          >>I get a compiler error "C2102 '&' requires l-value".
          >>
          >>I had thought that these should be the same. Clearly they are not, but Im
          >>not sure why.[/color]
          >
          >
          > Others have already answered. I just wanted to point out that your question
          > is entirely about addresses and pointers, and is completely unrelated to
          > references.[/color]


          good point ...

          I was thinking of mentioning that, but I decided to keep from getting
          too confusing.

          For example:

          int x = 10;

          int & Func()
          {
          return x;
          }

          int main()
          {
          Func() = 11;

          int * p = & ( Func() );

          }

          Note that in this case, Func() is returning a "reference" - in this case
          it IS an lvalue. You need to be extra careful that the reference is
          pointing to an object that is not destoyed for the life of the reference.



          Comment

          Working...