如何存储指向在 Fsm 中接受参数的函数的指针?
how to store pointer to function that takes arguments in Fsm?
我想在 FSM 中实现一个带有参数的函数。
当我尝试这个时,出现这个错误
error: initializer element is not constant
{(&DriveCenter)(86),50,{stop,right,left,stop}},
note: (near initialization for 'fsm[0].fun')
error: initializer element is not constant
{(&DriveRight)(45),50,{stop,right,left,stop}},
代码如下:
void DriveCenter(unsigned long out){
printf("\ncenter = %d",out);
}
typedef struct {
void (*fun)(unsigned long out);
unsigned long delay;
unsigned long Next_State[4];
} state ;
state fsm[4] ={
{(&DriveCenter)(86),50,{stop,right,left,stop}},
{(&DriveRight)(45),50,{stop,right,left,stop}},
{(&DriveLeft)(787),50,{stop,right,left,stop}},
{(&DriveStop)(33),50,{stop,right,left,stop}}
};
初始化函数指针时不能设置参数,参数应声明为struct
的另一个成员
typedef struct {
void (*fun)(unsigned long);
unsigned long out;
unsigned long delay;
unsigned long Next_State[4];
} state ;
state fsm[4] = {
{DriveCenter,86,50,{stop,right,left,stop}},
{DriveRight,45,50,{stop,right,left,stop}},
{DriveLeft,787,50,{stop,right,left,stop}},
{DriveStop,33,50,{stop,right,left,stop}}
};
在 C11 下,您可以使用匿名 struct
s 来说明这两个变量一起工作:
typedef struct {
struct {
void (*fun)(unsigned long);
unsigned long out;
};
unsigned long delay;
unsigned long Next_State[4];
} state ;
state fsm[4] = {
{{DriveCenter,86},50,{stop,right,left,stop}},
{{DriveRight,45},50,{stop,right,left,stop}},
{{DriveLeft,787},50,{stop,right,left,stop}},
{{DriveStop,33},50,{stop,right,left,stop}}
};
我想在 FSM 中实现一个带有参数的函数。 当我尝试这个时,出现这个错误
error: initializer element is not constant
{(&DriveCenter)(86),50,{stop,right,left,stop}},
note: (near initialization for 'fsm[0].fun')
error: initializer element is not constant
{(&DriveRight)(45),50,{stop,right,left,stop}},
代码如下:
void DriveCenter(unsigned long out){
printf("\ncenter = %d",out);
}
typedef struct {
void (*fun)(unsigned long out);
unsigned long delay;
unsigned long Next_State[4];
} state ;
state fsm[4] ={
{(&DriveCenter)(86),50,{stop,right,left,stop}},
{(&DriveRight)(45),50,{stop,right,left,stop}},
{(&DriveLeft)(787),50,{stop,right,left,stop}},
{(&DriveStop)(33),50,{stop,right,left,stop}}
};
初始化函数指针时不能设置参数,参数应声明为struct
typedef struct {
void (*fun)(unsigned long);
unsigned long out;
unsigned long delay;
unsigned long Next_State[4];
} state ;
state fsm[4] = {
{DriveCenter,86,50,{stop,right,left,stop}},
{DriveRight,45,50,{stop,right,left,stop}},
{DriveLeft,787,50,{stop,right,left,stop}},
{DriveStop,33,50,{stop,right,left,stop}}
};
在 C11 下,您可以使用匿名 struct
s 来说明这两个变量一起工作:
typedef struct {
struct {
void (*fun)(unsigned long);
unsigned long out;
};
unsigned long delay;
unsigned long Next_State[4];
} state ;
state fsm[4] = {
{{DriveCenter,86},50,{stop,right,left,stop}},
{{DriveRight,45},50,{stop,right,left,stop}},
{{DriveLeft,787},50,{stop,right,left,stop}},
{{DriveStop,33},50,{stop,right,left,stop}}
};