Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on funtion swap within the C and C++ forums, part of the Software Development category; How do you write a function swap?...
|
|||||||
|
|||
|
funtion swap
How do you write a function swap?
|
| The Following User Says Thank You to Twanawiliams For This Useful Post: | ||
| Sponsored Links |
|
|||
|
Re: funtion swap
you can use follwing code:
#include<stdio.h> #include<conio.h> void swap(int *a,int *b) { int temp; temp=*a; *a=*b; *b=temp; printf("a=%d b=%d",*a,*b); } void main() { int a=5,b=8; swap(&a,&b); } |
|
|||
|
Re: funtion swap
Well, Swetha's method is proper, but only meant for integers.
Following is the swap function for any type like: int, char, structure, pointers etc. anything. template<class T> void swap(T &data1, T &data2) { T temp = data1; data1 = data2; data2 = temp; } |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Swap two numbers | Manojks | Brainteasers | 11 | 07-15-2009 12:22 PM |
| Function Swap | Twanawiliams | C and C++ | 2 | 05-16-2008 04:14 AM |
| Get output as swap | Geek_Guest | C and C++ | 1 | 11-19-2007 11:41 AM |