// File: fptr.C // // Exploring function pointers. #include // some simple functions int add3(int n) { return n + 3 ; } int add5(int n) { return n + 5 ; } // Takes a function parameter int apply (int addx(int), int m) { return addx(m) ; } main() { int m = 2, result ; // func is a variable that holds the address of a function // which takes an int parameter and returns an int. // int (*func)(int) ; printf("m + 3 = %d\n", add3(m) ) ; printf("m + 5 = %d\n", add5(m) ) ; printf("\n") ; result = apply(&add3, m) ; printf("Applying add3 to m = %d\n", result) ; result = apply(&add5, m) ; printf("Applying add5 to m = %d\n", result) ; printf("\n") ; func = &add3 ; result = apply(func, m) ; printf("Applying func to m = %d\n", result) ; func = &add5 ; result = apply(func, m) ; printf("Applying func to m = %d\n", result) ; }