使用表就是组织数据,使用的结构体类型,里面可以放数据和函数指针,然后定义一定数量的数组,然后for循环遍历。
#include "stdafx.h" #include <stdio.h> #include <string.h> #include <math.h> #include<stdlib.h> #include <dos.h> #include <conio.h> typedef enum _Event_Type { connect_usb_event, disconnect_usb_event, update_samples_event }Event_Type; typedef struct _event_process { Event_Type event; int(*event_process)(void* input, void* output); }Event_Process; static int connect_usb(void* input, void* output) { printf("connect_usb\n"); return 0; } static int disconnect_usb(void* input, void* output) { printf("disconnect_usb\n"); return 0; } static int update_samples(void* input, void* output) { printf("update_samples\n"); return 0; } static Event_Process event_process[] = { {connect_usb_event,connect_usb}, {disconnect_usb_event,disconnect_usb}, {update_samples_event,update_samples}, }; static int Cur_Event = 0; int main() { int i = 0; for (i = 0; i<sizeof(event_process)/sizeof(Event_Process); i++) { if (Cur_Event == event_process[i].event) { ((event_process[i].event_process))(NULL, NULL); } } getchar(); return 0; }
原文:https://www.cnblogs.com/nowroot/p/12814898.html