#include <stdio.h> /* pf(void) pf是一个无参数函数 * pf(void) pf是一个无参数函数,它的返回值是一个指针 ( * pf(void) ) (void) pf是一个无参数函数,它的返回值是一个无参数函数的指针 * ( * pf(void) ) (void) pf是一个无参数函数,它的返回值是一个无参数函数的指针,这个函数的返回值又是一个指针 ( * ( * pf(void) ) (void) ) (void) pf是一个无参数函数,它的返回值是一个无参数函数的指针,这个函数的返回值又是一个无参数函数的指针 int ( * ( * pf(void) ) (void) ) (void) pf是一个无参数函数,它的返回值是一个无参数函数的指针,这个函数的返回值又是一个无参数且返回值为int的函数的指针。 */ /* int pf(void) int *pf(void) pf无参数函数,返回int*指针 int ( * pf(void) ) (void) pf无参数函数,返回一个无参数函数的指针,该函数返回int类型 int * ( * pf(void) ) (void) pf无参数函数,返回值是无参数函数的指针,该函数又返回一个int*指针 int ( * ( * pf(void) ) (void) ) (void) declare pf as function (void) returning pointer to function (void) returning pointer to function (void) returning int void ( * ( * pf(void) ) (void) ) (void) declare pf as function (void) returning pointer to function (void) returning pointer to function (void) returning void */ int myadd( int a, int b) { int z = a + b; return z; } int mysub(int a, int b) { int z = a - b; return z; } int mymul(int a, int b) { int z = a*b; return z; } int mydiv(int a, int b) { int z = a/b; return z; } //array of function pointers, int (* opfunctptr [ ] ) ( int x, int y) = { myadd, mysub, mymul, mydiv }; typedef int (*calc)(int x, int y ); //function returning the function pointer of type int (*calc)(int x, int y ) calc retmathfunc(int index) { return opfunctptr[index]; } int main(int argc, char* argv[]) { int choice, p1, p2, res; int (*calculator)(int x, int y); printf("Type -1 to quit "); printf("Type 0 - add, 1 - sub, 2 - mul, 3 - div "); scanf("%d", &choice); while( choice != -1) { calculator = retmathfunc(choice); //returns function pointer printf("Param1 "); scanf("%d", &p1); printf("Param2 "); scanf("%d", &p2); res = calculator(p1, p2); //calling function pointer printf("res = %d ", res); printf("Type 0 - add, 1 - sub, 2 - mul, 3 - div "); scanf("%d", &choice); } return 0; }