/* File: fptr1.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 ; /* fptr is a variable that holds the address of a function which takes an int parameter and returns an int. */ int (*fptr)(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, result = %d\n", result) ; result = apply(&add5, m) ; printf("Applying add5 to m, result = %d\n", result) ; printf("\n") ; fptr = &add3 ; result = apply(fptr, m) ; printf("Applying fptr to m, result = %d\n", result) ; fptr = &add5 ; result = apply(fptr, m) ; printf("Applying fptr to m, result = %d\n", result) ; printf("\n") ; result = (*fptr) (10) ; /* function application has higher precedence */ printf("Applying fptr to 10, result = %d\n", result) ; }