Home > C++ > swap function using pointer

swap function using pointer


#include <iostream.h>

void swap(int * , int *);

using namespace std;

int main(int argc, char *argv[])
{
    int n1 , n2 ;
    cout << " n1 = " ;
    cin >> n1 ;
    cout << endl << " n2 = ";
    cin >> n2;
    cout << endl;
    swap(&n1,&n2);
    cout << "After swap " << endl << " n1 = " << n1 << endl<< " n2 = " << n2 << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

void swap(int *num1, int *num2)
{
     int temp = 0;
     temp = *num1;
     *num1 = *num2;
     *num2 = temp;
}
Categories: C++
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: