correct usage of pointers?

E

Eli Luong

Hi,

Sorry if I accidentally posted a blank post before.

But, I was wondering, I'm practicing with pointers right now, and
wanted to know if I am using it correctly in the following code. By
correct I mean it follows convention, it should be the way I'm using
pointers to do the swapping thing I'm trying below.

Thanks.


#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

// prototype functions
void swap(double *x, double *y);

int main()
{
double a = 10;
double b = 20;
double *ptr1 = &a;
double *ptr2 = &b;

cout << a << " " << b << endl;
swap(ptr1, ptr2);
cout << a << " " << b << endl;

//system("pause");
return 0;
}

void swap(double *x, double *y)
{
double temp = *x;
*x = *y;
*y = temp;
}
 
A

a.a.makarov

Eli Luong пиÑал(а):
Hi,

Sorry if I accidentally posted a blank post before.

But, I was wondering, I'm practicing with pointers right now, and
wanted to know if I am using it correctly in the following code. By
correct I mean it follows convention, it should be the way I'm using
pointers to do the swapping thing I'm trying below.

Thanks.


#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

// prototype functions
void swap(double *x, double *y);

int main()
{
double a = 10;
double b = 20;
double *ptr1 = &a;
double *ptr2 = &b;

cout << a << " " << b << endl;
swap(ptr1, ptr2);
cout << a << " " << b << endl;

//system("pause");
return 0;
}

void swap(double *x, double *y)
{
double temp = *x;
*x = *y;
*y = temp;
}

Hi.
Sorry for my bad english.
Your swap function do nothing for the caller, because you pass
arguments by value.
Look at the function prototype
void swap(double *x, double *y);

Passing by reference solve the problem

// prototype functions
void swap(double *& x, double *& y);
//...
void swap(double *& x, double *& y)
{
double temp = *x;
*x = *y;
*y = temp;
}
 
A

a.a.makarov

(e-mail address removed) пиÑал(а):
Eli Luong пиÑал(а):


Hi.
Sorry for my bad english.
Your swap function do nothing for the caller, because you pass
arguments by value.
Look at the function prototype


Passing by reference solve the problem

// prototype functions
void swap(double *& x, double *& y);
//...
void swap(double *& x, double *& y)
{
double temp = *x;
*x = *y;
*y = temp;
}

Ooops... :eek:)
My comprehension of question is wrong... Sorry.
If you want to swap the values at wich x and y point your code is
right...
// prototype functions
void swap(double * x, double * y);
//...
void swap(double * x, double * y)
{ // swap values
double temp = *x;
*x = *y;
*y = temp;
}

To swap pointers the code look like this
// prototype functions
void swap(double *& x, double *& y);
//...
void swap(double *& x, double *& y)
{ // swap pointers
double* temp = x;
x = y;
y = temp;
}

Best regards.
 
E

Eli Luong

To swap pointers the code look like this
// prototype functions
void swap(double *& x, double *& y);
//...
void swap(double *& x, double *& y)
{ // swap pointers
double* temp = x;
x = y;
y = temp;
}

Best regards.

Swapping pointers means you're going to change the memory address to
which the pointer points to, right? So since you're trying to modify
something inside a function, I know you use the & symbol to reference
it, and then you just add the * symbol before it to mean it's a pointer
type?
 
A

a.a.makarov

Eli Luong пиÑал(а):
Swapping pointers means you're going to change the memory address to
which the pointer points to, right? So since you're trying to modify
something inside a function, I know you use the & symbol to reference
it, and then you just add the * symbol before it to mean it's a pointer
type?
Swapping pointers means you're going to change the memory address to
which the pointer points to, right? Yes.

So since you're trying to modify
something inside a function, I know you use the & symbol to reference
it, and then you just add the * symbol before it to mean it's a pointer
type?
" *& " that means reference to pointer. So you pass pointer by
reference not by value. In this case you alowed to change address to
which the pointer points to.

Fothemore you may use (and it's preferably) STL generic version of that
function.
It's better solution then write your own code.

#include <algorithm> // std::swap
#include <iostream> // std::cout

int main(int, char *[])
{
double a = 1.0;
double b = 2.0;
double* a_ptr = &a, b_ptr = &b;
std::cout<<"befor swapping: "<<"a = "<<*a_ptr<<", b =
"<<*b_ptr<<std::endl;
// swap pointers
std::swap(a_ptr, b_ptr);
std::cout<<"after swapping: "<<"a = "<<*a_ptr<<", b =
"<<*b_ptr<<std::endl;

double c = 3.0;
double d = 4.0;
std::cout<<"befor swapping: "<<"c = "<<c<<", d = "<<d<<std::endl;
// swap variable values
std::swap(a_ptr, b_ptr);
std::cout<<"after swapping: "<<"c = "<<c<<", d = "<<d<<std::endl;
return 0;
}

Best regards,
Andrey
 
E

Eli Luong

" *& " that means reference to pointer. So you pass pointer by
reference not by value. In this case you alowed to change address to
which the pointer points to.

Fothemore you may use (and it's preferably) STL generic version of that
function.
It's better solution then write your own code.

Thanks. I was just practicing to understand how pointers are working. I
was writing up another little bit of code. In my other piece of code,
the swapping worked correctly, but I was working with the dereferenced
values. In this one, it's working directly with pointers, swapping the
addresses of the pointers themselves, but once the function ends, it
doesn't seem to have swapped at all, even though it's showing it being
swapped in the function. I'm not sure how to look at this, because the
general function parameters are exactly the same, it's just how I'm
assigning the values inside the swap function that are different. Am I
working with copies. I'm not sure if pointers are passed as new copies
or as references.

The line (int *temp = p1;) should be assigning to temp the same address
that p1 is pointing to, along with the other code, so in the end p1 and
p2 should have their dereferenced values exchanged, but that is not the
case right now and I don't see why. Can you give some hints or
something?

// prototype functions
void swap(int *p1, int *p2);

int main()
{
int val1 = 10;
int val2 = 30;
int *ptr1 = &val1;
int *ptr2 = &val2;
swap(ptr1, ptr2);
cout << val1 << val2 << endl;
cout << *ptr1 << *ptr2 << endl;

//system("pause");
return 0;
}

void swap(int *p1, int *p2)
{
int *temp = p1;
cout << *temp << " " << *p1 << endl;
p1 = p2;
cout << *p1 << *p2 << endl;
p2 = temp;
cout << *p1 << *p2 << endl;
return;
}
 
H

Howard

Eli Luong said:
Hi,

Sorry if I accidentally posted a blank post before.

But, I was wondering, I'm practicing with pointers right now, and
wanted to know if I am using it correctly in the following code. By
correct I mean it follows convention, it should be the way I'm using
pointers to do the swapping thing I'm trying below.

Thanks.


#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

// prototype functions
void swap(double *x, double *y);

int main()
{
double a = 10;
double b = 20;
double *ptr1 = &a;
double *ptr2 = &b;

cout << a << " " << b << endl;
swap(ptr1, ptr2);
cout << a << " " << b << endl;

//system("pause");
return 0;
}

void swap(double *x, double *y)
{
double temp = *x;
*x = *y;
*y = temp;
}


That code is fine.

You don't actually need to create the pointers ptr1 and ptr2, though. You
could just call "swap(&a,&b);", if you want.

-Howard
 
M

Mak

Eli Luong пиÑал(а):
Thanks. I was just practicing to understand how pointers are working. I
was writing up another little bit of code. In my other piece of code,
the swapping worked correctly, but I was working with the dereferenced
values. In this one, it's working directly with pointers, swapping the
addresses of the pointers themselves, but once the function ends, it
doesn't seem to have swapped at all, even though it's showing it being
swapped in the function. I'm not sure how to look at this, because the
general function parameters are exactly the same, it's just how I'm
assigning the values inside the swap function that are different. Am I
working with copies. I'm not sure if pointers are passed as new copies
or as references.

The line (int *temp = p1;) should be assigning to temp the same address
that p1 is pointing to, along with the other code, so in the end p1 and
p2 should have their dereferenced values exchanged, but that is not the
case right now and I don't see why. Can you give some hints or
something?

// prototype functions
void swap(int *p1, int *p2);

int main()
{
int val1 = 10;
int val2 = 30;
int *ptr1 = &val1;
int *ptr2 = &val2;
swap(ptr1, ptr2);
cout << val1 << val2 << endl;
cout << *ptr1 << *ptr2 << endl;

//system("pause");
return 0;
}

void swap(int *p1, int *p2)
{
int *temp = p1;
cout << *temp << " " << *p1 << endl;
p1 = p2;
cout << *p1 << *p2 << endl;
p2 = temp;
cout << *p1 << *p2 << endl;
return;
}

Your "swap" function receives it's arguments (p1, p2) by value. So the
function operates with local copy of p1 and p2 of type pointer to int
(int*). All changes occures inside function "swap" not inside outer
scope.
So if you want to swap two pointers this is wrong solution.
You should change your function like this

// prototype functions
void swap(int *& p1, int *& p2);
//...
void swap(int *& p1, int *& p2)
{
int *temp = p1;
cout << *temp << " " << *p1 << endl;
p1 = p2;
cout << *p1 << *p2 << endl;
p2 = temp;
cout << *p1 << *p2 << endl;
return;
}

In this example "swap" function receive it's arguments p1 and p2. Where
p1 and p2 of type nt*& (reference to pointer to int). So the function
operates with local copy of reference to pointer to int (int*&) not
with local copy of pointers to int (int*).
Keep in mind value of pointer is address of memory to wich it points.

Anothe way to swap the values of two pointers.
// prototype functions
void swap(int ** p1, int ** p2);
//...
void swap(int ** p1, int ** p2)
{
int *temp = *p1;
*p1 = *p2;
*p2 = temp;
}
In this case you should call function like this
//...
int a = 0, b = 1;
int* ptr1 = &a;
int* ptr2 = &b;
swap(&ptr1, &ptr2);
//...
In this example swap function recieves it's aguments p1 and p2 of type
pointer to pointer to int (int**). So we can change the values to wich
they points to.

Best regards,
Andrey
 

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,998
Messages
2,570,242
Members
46,834
Latest member
vina0631

Latest Threads

Top