understanding pointer-swapping

Discussion in 'C++' started by arnuld, Aug 13, 2007.

  1. arnuld

    arnuld Guest

    it works fine:

    /* C++ Primer - 4/e
    *
    * example from section 7.2.2, pointer-swap
    * STATEMENT
    * in a function call where parameters are pointers, we actually copy
    the pointers.
    * here in this example we are using the original pointers.
    *
    */

    #include <iostream>

    void pointer_swap(int*& rp1, int*& rp2)
    {
    int* temp = rp2;
    rp2 = rp1;
    rp1 = temp;

    }


    int main()
    {
    int i = 1;
    int j = -1;

    int* pi = &i;
    int* pj = &j;

    std::cout << "before swapping pointers:\t*pi: "
    << *pi
    << "\t*pj: "
    << *pj
    << "\n\n";

    pointer_swap( pi, pj );

    std::cout << "after swapping pointers:\t*pi: "
    << *pi
    << "\t*pj: "
    << *pj
    << std::endl;

    return 0;
    }


    the only thing i do not understand is the line "int* temp = rp2;".
    lvale is a pointer-to-int and rvalue is a reference-to-pointer-to-int,
    they are two different types. why the expression work then ?
    arnuld, Aug 13, 2007
    #1
    1. Advertising

  2. arnuld

    Ian Collins Guest

    arnuld wrote:
    >
    > the only thing i do not understand is the line "int* temp = rp2;".
    > lvale is a pointer-to-int and rvalue is a reference-to-pointer-to-int,
    > they are two different types. why the expression work then ?
    >

    To help make things clearer, do the example with int rather than pointer
    to int. You should then realise that assigning from a reference to a
    type to a variable of the type is OK.

    --
    Ian Collins.
    Ian Collins, Aug 13, 2007
    #2
    1. Advertising

  3. arnuld wrote:
    ....
    > void pointer_swap(int*& rp1, int*& rp2)
    > {
    > int* temp = rp2;
    > rp2 = rp1;
    > rp1 = temp;
    >
    > }

    ....
    > the only thing i do not understand is the line "int* temp = rp2;".
    > lvale is a pointer-to-int and rvalue is a reference-to-pointer-to-int,
    > they are two different types. why the expression work then ?



    For the same reason this works:

    void int_swap(int & r1, int & r2)
    {
    int temp = r2; // make a copy of r2
    r2 = r1; // copy r1 into r2
    r1 = temp; // copy the original r2 value to r1
    }

    int & r1 - means r1 is a reference to a "non const" int from that point
    "r1" is an "alias" to some int storage somewhere. You can read and
    store values to it. The "int temp = r2" makes a copy. The other two
    statements perform stores to the references passed in.
    Gianni Mariani, Aug 13, 2007
    #3
  4. arnuld

    Jim Langston Guest

    "arnuld" <> wrote in message
    news:...
    > it works fine:
    >
    > /* C++ Primer - 4/e
    > *
    > * example from section 7.2.2, pointer-swap
    > * STATEMENT
    > * in a function call where parameters are pointers, we actually copy
    > the pointers.
    > * here in this example we are using the original pointers.
    > *
    > */
    >
    > #include <iostream>
    >
    > void pointer_swap(int*& rp1, int*& rp2)
    > {
    > int* temp = rp2;
    > rp2 = rp1;
    > rp1 = temp;
    >
    > }
    >
    >
    > int main()
    > {
    > int i = 1;
    > int j = -1;
    >
    > int* pi = &i;
    > int* pj = &j;
    >
    > std::cout << "before swapping pointers:\t*pi: "
    > << *pi
    > << "\t*pj: "
    > << *pj
    > << "\n\n";
    >
    > pointer_swap( pi, pj );
    >
    > std::cout << "after swapping pointers:\t*pi: "
    > << *pi
    > << "\t*pj: "
    > << *pj
    > << std::endl;
    >
    > return 0;
    > }
    >
    >
    > the only thing i do not understand is the line "int* temp = rp2;".
    > lvale is a pointer-to-int and rvalue is a reference-to-pointer-to-int,
    > they are two different types. why the expression work then ?


    Would it be easier to understand if it was:

    void pointer_swap(int** rp1, int** rp2)
    {
    int* temp = *rp2;
    *rp2 = *rp1;
    *rp1 = temp;
    }

    A reference makes it easier to understand what is going on, when you use a
    reference it is exactly like using the original variable (for most cases).

    So when rp1 is int*& rp1, rp1 is then a reference to an int pointer. When
    you use rp1, you are using the original int pointer.

    Changing rp1 iside the function, then, changes whatever variable was passed
    as the parameter, since a reference is an alias.

    Understand?
    Jim Langston, Aug 13, 2007
    #4
  5. arnuld

    arnuld Guest

    > On Aug 13, 3:31 pm, "Jim Langston" <> wrote:

    > Would it be easier to understand if it was:
    >
    > void pointer_swap(int** rp1, int** rp2)
    > {
    > int* temp = *rp2;
    > *rp2 = *rp1;
    > *rp1 = temp;
    >
    > }


    i tried and came to the conclusion that we are changing the original
    pointers themselves to point then to some other place. we are not
    changing the values pointed by pointers, we are changing the location
    of where pointers are pointing...

    am i right ?

    if i am right, then it was easier than understanding references to
    pointers.

    if i am wrong, then Aye.... sorry.

    > A reference makes it easier to understand what is going on, when you use a
    > reference it is exactly like using the original variable (for most cases).
    >
    > So when rp1 is int*& rp1, rp1 is then a reference to an int pointer. When
    > you use rp1, you are using the original int pointer.
    >
    > Changing rp1 iside the function, then, changes whatever variable was passed
    > as the parameter, since a reference is an alias.
    >
    > Understand?



    yep :)
    arnuld, Aug 13, 2007
    #5
  6. Hi!

    arnuld schrieb:
    > i tried and came to the conclusion that we are changing the original
    > pointers themselves to point then to some other place. we are not
    > changing the values pointed by pointers, we are changing the location
    > of where pointers are pointing...
    >
    > am i right ?


    Yes, you are.

    > if i am right, then it was easier than understanding references to
    > pointers.


    So, the functions do the same: swap the location the pointers point to.
    Once done with "int**" and once using "int*&".

    Frank
    Frank Birbacher, Aug 13, 2007
    #6
    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. Replies:
    10
    Views:
    663
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    815
    Jordan Abel
    Mar 28, 2006
  3. Howard
    Replies:
    3
    Views:
    259
    Howard
    Dec 5, 2007
  4. Replies:
    4
    Views:
    1,218
    Fred Zwarts
    Jul 2, 2009
  5. A
    Replies:
    7
    Views:
    620
Loading...

Share This Page