C语言函数指针数组
怎样定义?
函数指针定义:
int (*g_pfunc)(int , int ); //定义一个函数指针
也可以带形参,如同这样:
int (*g_pfunc)(int a , int b ); //定义一个函数指针
函数指针数组定义:
int (*g_pfunc[9])(int , int ); //定义一个函数指针数组
int (*g_pfunc[9])(int a , int b); //定义一个函数指针数组
将函数指针定义成一个自定义类型
typedef int (*cmd_handle_t)(char (*args)[20] ); //命令处理函数通用形式
typedef int (*cmd_handle_t)(char (*args)[20] ); //命令处理函数通用形式
typedef struct {
char *cmd_name; //命令名称
cmd_handle_t handle; //命令处理函数
}shell_cmd_t;
/* all of the shell cmd regist in this table */
shell_cmd_t g_shell_cmd_table[SHELL_CMD_TOTAL_QUANTITY] = {
{"reboot", cmdReboothandle},
{"setip", setIPhandle},
{"setport", setPorthandle},
{"setlinkid", setLinkIDhandle},
{"setmac", setLinkMAChandle},
{"setimei", setIMEIhandle},
{"setkey", setLogkeyhandle},
{"setfreq", setFreqhandle},
{"setpwm", setPWMhandle},
{"setjzv", setJZpowerhandle},
{"setmsk", setmskhandle},
{"setsensor", setsensorhandle},
{"getfreq", getfreqhandle}
};
int setLinkIDhandle(char (*arg)[20])
{
int id;
sscanf( arg[0] , "%x" , &id );
g_paremeters.linkid = (u8)id;
write_parametersToEEPROM(&(g_paremeters.linkid), sizeof(g_paremeters.linkid));
return 0;
}
示例:
int (*g_pfunc[9])(int , int ); //定义一个函数指针数组
int test1(int a, int b)
{
return a+b;
}
void testtest()
{
g_pfunc[0] = test1; //函数指针赋值
g_pfunc[0](2,3); //使用函数指针,效果与直接调函数一样
}
以上均实测通过
版权声明:本文为u010243305原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。