#include #include // Take in a pointer-to-a-function, two arguments for the function, // and apply the function. Return the result. double applyFunction (double (*func)(double, double), double x, double y) { double z; // For result. z = (*func) (x, y); // Apply the function. Note the parentheses around "*func". return z; } // A typedef for a function-pointer: typedef double (*funcPointerType)(double, double); // getFunction is declared as a function that returns a funcPointerType, // and is declared to itself take a char* parameter. funcPointerType getFunction(char *str); int main () { double x, y, z; double (*func)(double, double); x = 1.0; y = 2.0; // Test values. func = getFunction ("max"); // Get back a function-pointer. z = applyFunction (func, x, y); // Send the function-pointer to another function. printf ("x = %lf y = %lf max(x,y) = %lf\n", x, y, z); } //--------------------------------------------------------------------- // The function implementations: double max (double x, double y) { if (x > y) return x; else return y; } double min (double x, double y) { if (x < y) return x; else return y; } double absDiff (double x, double y) { if (x > y) return (x - y); else return (y - x); } // Function that returns a function-pointer funcPointerType getFunction (char *str) { if (strcmp (str, "max") == 0) { return max; } else if (strcmp (str, "min") == 0) { return min; } else if (strcmp (str, "absDiff") == 0) { return absDiff; } }