函数指针:函数指针数组的使用:
不带参数和返回值的函数指针:
#include <stdio.h> #include <stdlib.h> //定义一个没有返回值也没有入口参数的函数指针 typedef void (CONTROL_FLOW_FUNCTION)(); void Function1() { printf(" This is printed by function1 !!!\n"); } void Function2() { printf(" 22222222\n"); printf(" 22222222\n"); } void Function3() { printf(" This is printed by function3......\n"); } CONTROL_FLOW_FUNCTION *ControlFlowFunctions[] = { Function1, Function2, Function3, NULL }; char *ControlFlowFunctionNames[] = { "Function1", "Function2", "Function3", NULL }; int main() { ; printf("main start!\n"); ; ControlFlowFunctions[i]!=NULL; i++){ printf("ControlFlow.%s\n", ControlFlowFunctionNames[i]); ControlFlowFunctions[i](); } printf("main end!\n"); }
带参数和返回值的函数指针:
#include <stdio.h> #include <stdlib.h> //定义一个函数指针类型 typedef int (CONTROL_FILTER_FUNCTION)(int dat); int Function1(int data) { printf("Check if data can be exactly divisible by 2.\n"); == ) ; else ; } int Function2(int data) { printf("Check if data can be exactly divisible by 3.\n"); == ) ; else ; } int Function3(int data) { printf("Check if data can be exactly divisible by 5.\n"); == ) ; else ; } CONTROL_FILTER_FUNCTION *ControlFilterFunctions[] = { Function1, Function2, Function3, NULL }; char *ControlFilterFunctionNames[] = { "Function1", "Function2", "Function3", NULL }; int main() { ; ; printf("==Check if data can be exactly divisible by 2 or 3 or 5==.\n"); ; ControlFilterFunctions[i]!=NULL; i++){ printf("ControlFilter.%s\n", ControlFilterFunctionNames[i]); if(ControlFilterFunctions[i](data)) { printf("YES::%d can be exactly divisible by 2 or 3 or 5.\n", data); ; } } printf("NO::%d can't be exactly divisible by 2 or 3 or 5.\n", data); ; }