/* File: fptr2.c Exploring function pointers. */ #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) ; printf("After add3(&m), m = %d\n", m) ; add5(&m) ; printf("After add5(&m), m = %d\n", m) ; printf("\n") ; result = apply(&add3, m) ; printf("Applying add3 to m, result = %d\n", result) ; printf("m = %d\n", m) ; result = apply(&add5, m) ; printf("Applying add5 to m, result = %d\n", result) ; printf("m = %d\n", m) ; printf("\n") ; fptr = &add3 ; (*fptr) (&m) ; /* function application has higher precedence */ printf("Applying fptr to m, m = %d\n", m) ; }