// testing multiple functions at two points // using a table and pointer // and function pointer arguments to a function // #include #include double my_sqrt( double x) { return exp(log(x)/2); } double my_cbrt( double x) { return exp(log(x)/3); } double my_atan( double x) { return atan2(x,1); } typedef double (*Func)(double); // Func is type of variable void test( const char *s, Func f, Func g, double x) { double y1 = f(x); // or: (*f)(x) double y2 = g(x); printf( "%s %g %g %g\n", s, x, y1, y2); } struct list { char *s; Func f, g; double x[2]; } table[] = { { "sqrt", my_sqrt, sqrt, { 2, 10} }, { "cbrt", my_cbrt, cbrt, { 2, 10} }, { "atan", my_atan, atan, { 1, 23} }, { 0, 0, 0, { 0 } } }; int main( void) { for( struct list *p = table; p->s != 0; ++p) { for( int i = 0; i < 2; ++i) { test( p->s, p->f, p->g, p->x[i]); } } } /* output: sqrt 2 1.41421 1.41421 sqrt 10 3.16228 3.16228 cbrt 2 1.25992 1.25992 cbrt 10 2.15443 2.15443 atan 1 0.785398 0.785398 atan 23 1.52735 1.52735 */