473,382 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 ?

Aug 13 '07 #1
5 1858
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
24
by: Norm | last post by:
Could someone explain for me what is happening here? char *string; string = new char; string = "This is the string"; cout << string << std::endl; The output contains (This the string) even...
3
by: dj | last post by:
I've read section 6 of the FAQ, but still am a bit confused... Please refer to the comments in the following code for my questions: -----------------------------------------------------------...
26
by: Bail | last post by:
I will have a exam on the oncoming friday, my professor told us that it will base upon this program. i am having troubles understanding this program, for example what if i want to add all the...
3
by: Miguel | last post by:
Hi, I'm new to .NET and am using VB .NET as I am from a VB background. I am having difficulty understanding the way .NET handles, passes and uses objects. I have had a look at the Micrsoft Data...
4
by: bitshadow | last post by:
I've been working on a link list implementation and I'm driving myself crazy trying to understand something. assuming i have a list and i have the following quasi pseudocode: add(list *head):...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
13
by: Sri Harsha Dandibhotla | last post by:
Hello all. I recently came across a function declaration as : char(*(*x()))(); This was not in some code but it was in a C questions thread on some group. I tried to decipher what it returns but...
12
by: kevin.eugene08 | last post by:
Hello all, Forgive the somewhat simple question I am sure this will be, but I am having some problem understanding what: char **argv looks like. For instance, i know through trial and error...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.