简单的C语言示例:
#include <stdio.h> enum { LED1, LED2, LED3, LED_NUM }; void Func_LED1(unsigned char state); void Func_LED2(unsigned char state); void Func_LED3(unsigned char state); /** * 函数指针数组定义格式 * 函数返回类型 (*指针变量名[数组大小])(函数参数列表); * @param state * @return * */ void (*func[LED_NUM])(unsigned char state) = {Func_LED1, Func_LED2, Func_LED3}; void Func_LED1(unsigned char state) { if (state) printf("LED1 ON\r\n"); else printf("LED1 OFF\r\n"); } void Func_LED2(unsigned char state) { if (state) printf("LED2 ON\r\n"); else printf("LED2 OFF\r\n"); } void Func_LED3(unsigned char state) { if (state) printf("LED3 ON\r\n"); else printf("LED3 OFF\r\n"); } int main() { // for (int i = 0; i < LED_NUM; ++i) { // func[i](1); // } func[LED1](1); func[LED2](1); func[LED3](1); return 0; }
运行结果: