c - 函数指针处理程序
问题描述
我正在尝试为 Initialise 系统编写函数,同时使用系统索引和值插入用于系统触发处理的函数指针 那么如何访问变量index
和函数val
System_Init
typedef void (*System_Handler)(unsigned int short indx, char val);
void System_Init(Switch_Handler sw_hdl)
{
unsigned short int test;
test = indx;
/* Need to access variables indx and val Here . How can we do ?*/
}
解决方案
函数指针不与一组函数参数捆绑在一起。您必须单独提供参数,通常通过将它们与函数指针一起传递:
typedef void (*System_Handler)(unsigned int short indx, char val);
void System_Init(System_Handler sw_hdl, unsigned short indx, char val)
{
unsigned short int test;
test = indx;
//...
sw_hdl(indx,val);
}