Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > correct usage of pointers?

Reply
Thread Tools

correct usage of pointers?

 
 
Eli Luong
Guest
Posts: n/a
 
      06-08-2006
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;
}

 
Reply With Quote
 
 
 
 
a.a.makarov@gmail.com
Guest
Posts: n/a
 
      06-08-2006

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;
}

 
Reply With Quote
 
 
 
 
a.a.makarov@gmail.com
Guest
Posts: n/a
 
      06-08-2006

(E-Mail Removed) писал(а):

> 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;
> }


Ooops... )
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.

 
Reply With Quote
 
Eli Luong
Guest
Posts: n/a
 
      06-08-2006
>
> 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?

 
Reply With Quote
 
a.a.makarov@gmail.com
Guest
Posts: n/a
 
      06-08-2006

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?


> 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

 
Reply With Quote
 
Eli Luong
Guest
Posts: n/a
 
      06-08-2006

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


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;
}

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      06-08-2006

"Eli Luong" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
Mak
Guest
Posts: n/a
 
      06-09-2006

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.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
Uploaded File Empty but in correct folder with correct name froil Perl Misc 12 03-02-2006 01:21 PM
Correct White Balance Doesn't Mean Correct Color?? jim evans Digital Photography 28 12-27-2005 05:10 AM
correct or not correct? Dan HTML 7 10-02-2003 10:16 PM
To correct my program. please, check to find errors and correct me. joon Java 1 07-08-2003 06:13 AM



Advertisments