Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

swaping pointer values

Status
Not open for further replies.

jimberger

Programmer
Jul 5, 2001
222
GB
hello,

i am trying to swap a pair of pointer values. i can do this if i pass the the pointers in by value but is they a way i can do this when i pass the values in by reference.

thanks for your help
jim
 
Try using a template function - something like this should swap any two items:

[tt]template<class T> void swap(T& a,T& b)
{
T temp = a;
a = b;
b = temp;
}[/tt]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Gednick,

Wouldn't he need to do something like this to swap pointer values:

template<class T>
void swapPointers( T **p1, T **p2 )
{
T *temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}

where, for example, pointers are passed like this:

int *ptr1, *ptr2;

...

swapPointers( &ptr1, &ptr2 );

Maybe I'm misunderstanding the question.

PS- Where do you get all your smileys?

 
Actually, I guess he didn't need that! I tried out the function after I posted. Opps! All of this pointer stuff sometimes throws me for a loop still, but the above makes sense to me now.

I still want to know about the smileys.
 
Hi BoulderBum, as I found out a few weeks ago myself, you get the smileys from the link below called &quot;Emoticons/SMileys&quot; next the Email ntoification check box. There's a ton of different smileys in there that you can use.

[deejay]

As the template function I listed - recall that a pointer is simply an address in memory (a numeric value). Its actually no different than passing in a couple of 'ints' and swapping them! :)

[lightsaber]
May the force be with you!
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
thanks for your responses...

however i still have the problem
the swap function works well with ints as when you declare the variable temp it gets its own memory space and the assignment operator &quot;temp = a&quot; merely copies the contents of &quot;a&quot; into &quot;temp's&quot; memory address. with pointers however, when you do the assignment &quot;temp = a&quot; , temp will now point to what ever &quot;a&quot; points to - so if you change &quot;a&quot; you will also change temp - so temp cant be used as a buffer in this way which is the problem im having - i dont know if there is a solution ...is they way i can store the memory address as a int like you say then re-assign the pointer address with the int variables?
thanks for your time on this
jim
 
Hi Jim, what compiler are you using?? I've tried it on Visual C++ with several kinds of pointers and it works great!!

[flush2]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Gracias Gednick! [bandito]

Jimberger, both my function and Gednick's swapped pointer values successfully for me. When you have an integer &quot;a&quot; and assign a pointer (&quot;ptr1&quot;) to it, the pointer points to &quot;a&quot;'s memory address. When you define a second pointer (&quot;ptr2&quot;) and have an assignment like ptr2 = ptr1, then ptr2 points to &quot;a&quot; not ptr1. You can change the address ptr1 points to, and ptr2 will not be affected.

Try it out! I used cout << ptr1 << ' ' << ptr2 << endl; before and after the switch to check the address each pointer was assigned to.

[wiggle]
 
[cheers] No Problemo Boulder Bum! I see you got the smileys under control now.


[worm]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
If you cout your pointers, make sure they're not char pointers unless you like seeing random strings of characters.

Also, there's no need to write a new swap function; the STL already has one. Just #include <algorithm>.

There's also an iter_swap. It swaps what each iterator points to (and pointers are considered iterators). I'm not sure if this is the behavior you're looking for, or if you just want swap.

Finally, in your initial post, you say yo were able to swap when you pass in by value... I'm not sure how you would have accomplished this. Is it possible you're getting the correct results with the code that's been posted, but are comparing them to the results of your &quot;swap by value,&quot; which is actually incorrect?
 
I commented out the code that uses dereferenced pointers, the one I left is what I think you need. Both work nonetheless.


===<code>
#include <iostream.h>

void swapP(int* pa, int* pb);

int main()
{
int a = 5;
int b = 3;

int* pa = &a;
int* pb = &b;

cout << (*pa) << &quot; &quot; << (*pb) << endl;

swapP(&a, &b);

return 0;
}

void swapP(int* pa, int* pb)
{/*
int* temp = new int;

(*temp) = (*ca);
(*ca) = (*cb);
(*cb) = (*temp);

cout << (*ca) << &quot; &quot; << (*cb) << endl;

temp = NULL;
delete temp;
return;

*/
int* temp = new int;

temp = pa;
pa = NULL;

pa = pb;
pb = temp;

cout << (*pa) << &quot; &quot; << (*pb) << endl;

temp = NULL;
delete temp;
return;
}
</code>
Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top