C,指向struct中函数的指针,警告"从不兼容的指针类型赋值"

Len*_*nny 1 c struct pointers function-pointers

我有一个结构定义如下:

typedef struct {
    char (*behave) (int*);
} BEHAVIOURSTRUCT;
Run Code Online (Sandbox Code Playgroud)

这个结构是在.h文件中定义的,它包含在.c文件中我有一个全局变量(MAX_BEHAVIOURS定义为3):

BEHAVIOURSTRUCT bhvr[MAX_BEHAVIOURS];
Run Code Online (Sandbox Code Playgroud)

并在init中尝试将其设置为此,但在此我收到警告:"从不兼容的指针类型分配"

void init() {
bhvr[0].behave = BHVR_line_follow; // here
...
}
Run Code Online (Sandbox Code Playgroud)

我想要的功能

void BHVR_line_follow(int *currstate){
....
}
Run Code Online (Sandbox Code Playgroud)

通过它的声音我在结构中的声明和函数的指针不是来自同一个版本,但在我看来它们是.但很可能我错了.

abe*_*nky 6

这是您的功能,并且所需的功能类型是并排的:

void BHVR_line_follow(int *currstate)
char (*behave) (int*)
Run Code Online (Sandbox Code Playgroud)

需要的函数类型取a int*并返回a char.
你的函数接受int*并返回void(没有).

总而言之,返回类型BHVR_line_follow是错误的.