// File: fptr2.C // // Exploring function pointers. #include #include // some simple functions void add3(int *iptr) { *iptr = *iptr + 3 ; } void add5(int *iptr) { *iptr = *iptr + 5 ; } // Takes a function parameter int apply (void addx(int *), int r) { addx(&r) ; return r ; } main() { int m = 2, result ; // fptr is a variable that holds the address of a function // which takes an int pointer parameter and returns nothing. // void (*fptr)(int *) ; add3(&m) ; cout << "After add3(&m), m = " << m << endl ; add5(&m) ; cout << "After add5(&m), m = " << m << endl ; cout << endl ; result = apply(&add3, m) ; cout << "Applying add3 to m, result = " << result << endl ; cout << "m = " << m << endl ; result = apply(&add5, m) ; cout << "Applying add5 to m, result = " << result << endl ; cout << "m = " << m << endl ; cout << endl ; fptr = &add3 ; (*fptr) (&m) ; // function application has higher precedence cout << "Applying fptr to m, m = " << m << endl ; }