/* File: fptr3.c Exploring function pointers. This version does not compile with gcc. */ #include /* 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) ; printf("After add3(&m), m = %d\n", m) ; add5(&x) ; printf("After add5(&x), x = %lf\n", x) ; printf("\n") ; apply(&add3, &m) ; printf("Applying add3 to m, m = %d\n", m) ; apply(&add5, &x) ; printf("Applying add5 to x, x = %lf\n", x) ; printf("\n") ; fptr = &add3 ; (*fptr) (&m) ; printf("Applying fptr to m, m = %d\n", m) ; fptr = &add5 ; (*fptr) (&x) ; printf("Applying fptr to x, x = %lf\n", x) ; }