![]() |
How do I swap pointers with a function?
I need help with the following problem about swapping pointers.
The following code is in main: :
main()and thus printf prints "*xp = 4, *yp =3". I know how to swap int values by using a temp variable, but is it the same approach here? Any help or hints will be greatly appreciated!! Thank you!! |
Put one in a temp, put the second where the first was, and put the temp in the second.
|
This is what I have so far...am I on the right track? Please help!! You help is greatly appreciated!
void intpswap(int *xp, int *yp) { int t = *xp; *xp = *yp; *yp = t; } |
Why would you declare t as an int? You're swapping int pointers. Since you want persistent effects (I imagine), you need to pass pointers to the pointers to swap, since copies will simply go out of scope when the function returns.
Note that a and b are not swapped, despite what the printf SAYS, only the pointers are swapped. :
#include <stdio.h>Quote:
|
:
void swap( int *a, int *b)FWIW. I think he meant to swap what the pointers were "aimed" at. Not the actual pointers themselves. But it doesn't matter.... |
| All times are GMT -5. The time now is 2:01 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC