Swap parameter using pointer - C++ Data Type

C++ examples for Data Type:Pointer

Description

Swap parameter using pointer

Demo Code

#include <iostream>
using namespace std;
void swap(double *, double *);  // function prototype
int main()/*from w w w  . j  a va  2  s. c  o  m*/
{
   double firstnum = 20.5, secnum = 6.25;
   cout << "The value stored in firstnum is: " << firstnum << endl;
   cout << "The value stored in secnum is: " << secnum << "\n\n";
   swap(&firstnum, &secnum);     // call swap
   cout << "The value stored in firstnum is now: " << firstnum << endl;
   cout << "The value stored in secnum is now: " << secnum << endl;
   return 0;
}
// This function swaps the values in its two arguments
void swap(double *nm1Addr, double *nm2Addr)
{
   double temp;
   temp = *nm1Addr;      // save firstnum's value
   *nm1Addr = *nm2Addr;  // move secnum's value into firstnum
   *nm2Addr = temp;      // change secnum's value
   return;
}

Result


Related Tutorials