By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,615 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,615 IT Pros & Developers. It's quick & easy.

understanding pointer-swapping

P: n/a
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 ?

Aug 13 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Aug 13 '07 #2

P: n/a
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.
Aug 13 '07 #3

P: n/a
"arnuld" <ge*********@gmail.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.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?
Aug 13 '07 #4

P: n/a
On Aug 13, 3:31 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
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 :)

Aug 13 '07 #5

P: n/a
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
Aug 13 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.