//  File: fptr3.C
//
//  Exploring function pointers.

#include <iostream.h>
#include <iomanip.h>

// some simple functions

void add3(int *iptr) {
   *iptr = *iptr + 3 ;
}


void add5(double *dptr) {
   *dptr = *dptr + 5.0 ;
}


// Takes a function parameter

void apply (void addx(void *), void *rptr) {
   
  addx(rptr) ;
}


main() {
   int m = 2 ;
   double x = 5.0 ;

   // fptr is a variable that holds the address of a function
   // which takes a void * parameter and returns nothing.
   //
   void (*fptr)(void *) ;

   add3(&m) ;
   cout << "After add3(&m), m = " << m << endl ;
   add5(&x) ;
   cout << "After add5(&x), x = " << x << endl ;
   cout << endl ;

   apply(&add3, &m) ;
   cout << "Applying add3 to m, m = " << m << endl ; 
   apply(&add5, &x) ;
   cout << "Applying add5 to x, x = " << x << endl ; 
   cout << endl ;

   fptr = &add3 ;
   (*fptr) (&m) ; 
   cout << "Applying fptr to m, m = " << m << endl ;
   fptr = &add5 ;
   (*fptr) (&x) ; 
   cout << "Applying fptr to x, x = " << x << endl ;
}
