![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2007
Posts: 2
Rep Power: 0
![]() |
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()
{
int x=1,y=2;
int *xp,*yp;
.
.
.
xp = &x;
yp = &y;
intpswap(<arg1>,<arg2>);
x=3;y=4;
printf("*xp = %d, *yp = %d\n",*xp,*yp);
}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!! Last edited by Thiengineer; Feb 4th, 2007 at 6:09 PM. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Put one in a temp, put the second where the first was, and put the temp in the second.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2007
Posts: 2
Rep Power: 0
![]() |
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; } |
|
|
|
|
|
#4 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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>
void pSwap (int **a, int **b)
{
int *temp = *a;
*a = *b;
*b = temp;
}
int main ()
{
int a = 4;
int b = 6;
int *pA = &a;
int *pB = &b;
printf ("a: %d , b: %d\n", *pA, *pB);
pSwap (&pA, &pB);
printf ("(swapped) a: %d , b: %d\n", *pA, *pB);
}Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
void swap( int *a, int *b)
{
int t=*a;
*a=*b;
*b=t;
}
int main()
{
int x=1;
int y=2;
int *a=&x;
int *b=&y;
printf("%d %d\n", *a, *b);
swap(a,b);
printf("%d %d\n", *a, *b);
return 0;
}
kcsdev:/home/jmcnama> cc t.c
kcsdev:/home/jmcnama> ./a.out
1 2
2 1FWIW. I think he meant to swap what the pointers were "aimed" at. Not the actual pointers themselves. But it doesn't matter.... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Confusion With Pointers and Arrays | JawaKing00 | C | 10 | Sep 14th, 2006 7:33 AM |
| Functions returing a pointer using new | Jessehk | C++ | 29 | Dec 21st, 2005 2:52 AM |
| pointers are difficult | cjaime | C | 3 | Nov 12th, 2005 12:26 AM |
| Pointers in C (Part II) | Stack Overflow | C | 2 | Apr 29th, 2005 10:39 AM |
| Pointers in C (Part I) | Stack Overflow | C | 4 | Apr 28th, 2005 7:03 PM |