473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to swap pointers

I don't know how to approach this problem, please help!

The following code is in main:

Expand|Select|Wrap|Line Numbers
  1.  
  2. main()
  3.  
  4.         {  int x=1,y=2;
  5.            int *xp,*yp;
  6.            .
  7.            .
  8.            .
  9.            xp = &x;
  10.            yp = &y;
  11.            intpswap(<arg1>,<arg2>);
  12.            x=3;y=4;
  13.            printf("*xp = %d, *yp = %d\n",*xp,*yp); 
  14.         }
  15.  
Question:

Write a function called intpswap that swaps what xp and yp point at,
and thus printf prints "*xp = 4, *yp =3".
Feb 4 '07 #1
3 1563
horace1
1,510 Expert 1GB
post what you have done so far
Feb 4 '07 #2
This is what I have so far...am I on the right track? Please help!!

void intpswap(int *xp, int *yp)
{
int t = *xp;
*xp = *yp;
*yp = t;
}
Feb 5 '07 #3
willakawill
1,646 1GB
This is what I have so far...am I on the right track? Please help!!

void intpswap(int *xp, int *yp)
{
int t = *xp;
*xp = *yp;
*yp = t;
}
On the right track with some distance still to cover :)
with
Expand|Select|Wrap|Line Numbers
  1. int t = *xp;
You are assigning the value '1' to the int 't'
with
Expand|Select|Wrap|Line Numbers
  1. *xp = *yp;
you are assigning the value that yp points to, 2, to the memory space that xp points to, x. So x now equals 2
with
Expand|Select|Wrap|Line Numbers
  1. *yp = t;
you are assigning the value at t, 1, to where yp points to, y. So now y = 1
The original pointers xp and yp have not changed. In order to change them we have to pass a reference to them:
Expand|Select|Wrap|Line Numbers
  1. void intpswap(int **xp, int **yp)
  2. {
  3.    int *t = *xp;
  4.    *xp = *yp;
  5.    *yp = t;
  6. }
  7.  
  8. int main() {
  9.     int x=1,y=2;
  10.     int *xp,*yp;
  11.  
  12.     xp = &x;
  13.     yp = &y;
  14.     intpswap(&xp, &yp);
  15.  
  16.     x=3;y=4;
  17.     printf("*xp = %d, *yp = %d\n",*xp,*yp); 
  18.  
  19.     getchar();
  20.     return 0;
  21. }
  22.  
  23.  
see if you can tell us what is happening here
Feb 5 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

38
by: JKop | last post by:
union SignedChoice{ long with_sign; unsigned long without_sign; }; int main() { SignedChoice data;
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
10
by: Atlas | last post by:
To swap to nodes in list, changing pointers is enough. But the stl swap method seems to copy their values, doesn't it?
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
3
by: .rhavin grobert | last post by:
guess you have two classes (A and B) and you have two objects (C1 and C2) of a class C that is defined as class C: public A, public B can I *somehow* swap B of C1 and B of C2? To make myself...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.