understanding pointer-swapping



 DEVELOP > c-Plus-Plus > understanding pointer-swapping

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "arnuld"
Date: 13 Aug 2007 02:21:58 AM
Object: understanding pointer-swapping
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 ?
.

User: "Jim Langston"

Title: Re: understanding pointer-swapping 13 Aug 2007 05:31:23 AM
"arnuld" <geek.arnuld@gmail.com> wrote in message
news:1186989718.863847.212660@q3g2000prf.googlegroups.com...

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?
.
User: "arnuld"

Title: Re: understanding pointer-swapping 13 Aug 2007 06:39:52 AM

On Aug 13, 3:31 pm, "Jim Langston" <tazmas...@rocketmail.com> 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 :)
.
User: "Frank Birbacher"

Title: Re: understanding pointer-swapping 13 Aug 2007 07:23:52 AM
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
.



User: "Gianni Mariani"

Title: Re: understanding pointer-swapping 13 Aug 2007 02:34:46 AM
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.
.

User: "Ian Collins"

Title: Re: understanding pointer-swapping 13 Aug 2007 02:27:15 AM
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.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER