understanding pointer-swapping

A

arnuld

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 ?
 
I

Ian Collins

arnuld said:
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.
 
G

Gianni Mariani

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.
 
J

Jim Langston

arnuld said:
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?
 
A

arnuld

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 :)
 
F

Frank Birbacher

Hi!
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top