c - 在c中设置一系列函数指针和回调

标签 c pointers callback function-pointers

我已经阅读了一些关于函数指针作为回调的不同答案,但我自己实现它仍然遇到一些问题。

类似问题:How do function pointers in C work?

还有这个:Understanding typedefs for function pointers in C

还有其他一些。

我不确定这是否是最好的方法。

文件.h

typedef int (*reg_callback)(void*, void*); //generalize a bit?

int register_created(reg_callback cb);
int register_fixed_update(reg_callback cb);
int register_variable_update(reg_callback cb);
int register_render(reg_callback cb);
int register_pased(reg_callback cb);
int register_resume(reg_callback cb);
int register_resize(reg_callback cb);
int register_quit(reg_callback cb);

int init_engine(int width, int height, char *title, double fps);

//prototypes of functions that I need callbacks for atm maybe more soon
void created(void);
void fixed_update(void);
void variable_update(void);
void render(double alpha);
void paused(void);
void resumed(void);
void resized(int width, int height);
void quit(void);

文件.c

static int register_callback_function(int c_type, reg_callback cb)
{
    int success = -1;
    switch(c_type)
    {
        case 000000:
            printf("registering fixed update\n");
            break;
        case 000001:
            printf("registering variable update\n");
            break;
        case 000002:
            printf("registering render\n");
            break;
        case 000003:
            printf("registering pause\n");
            break;
        case 000004:
            printf("registering resume\n");
            break;
        case 000005:
            printf("registering resize\n");
            break;
        case 000006:
            printf("registering quit\n");
            break;
        default:break;
    }
    return success;
}
int register_fixed_update(reg_callback cb)     { return register_callback_function(000000, cb); };
int register_variable_update(reg_callback cb)  { return register_callback_function(000001, cb); };
int register_render(reg_callback cb)           { return register_callback_function(000002, cb); };
int register_pased(reg_callback cb)            { return register_callback_function(000003, cb); };
int register_resume(reg_callback cb)           { return register_callback_function(000004, cb); };
int register_resize(reg_callback cb)           { return register_callback_function(000005, cb); };
int register_quit(reg_callback cb)             { return register_callback_function(000006, cb); };
int register_created(reg_callback cb)          { return register_callback_function(000007, cb); };

void created(void)                  { //call create callback func }
void fixed_update(void)             { //call fixed update callback func}
void variable_update(void)          { //...}
void render(double alpha)           { //...}
void paused(void)                   { //...}
void resumed(void)                  { //...}
void resized(int width, int height) { //...}
void quit(void)                     { //...}

目前我收到如下警告:

warning: incompatible pointer types passing 'void (void)' to parameter of
      type 'reg_callback' (aka 'int (*)(void *, void *)') [-Wincompatible-pointer-types]
    register_created(created);
                     ^~~~~~~
note: passing argument to parameter 'cb' here
int register_created(reg_callback cb);

不兼容的指针类型,但我认为 void* 指针可以指向任何东西,甚至什么都没有?

我想概括函数指针,这样我就可以使用一个指针原型(prototype)来处理所有这些设置代码。从代码不工作我知道我正在以错误的方式处理它,并希望解决这个问题。

也许有一个带有一些函数指针的结构,但我不确定 atm。

最佳答案

我可以编译“gcc a.c”以下代码,而不会出现警告/错误。您将回调函数定义为“int fn(void *, void *)”。所以你的回调函数应该实现为“int fn(void *p, void *q){}”

我将函数“fixed_update”更改为“intfixed_update(void *a, void *b) {}”以匹配回调函数定义为 void 错误。

希望对你有帮助。

#include <stdio.h>

typedef int (*reg_callback)(void*, void*);

static int register_callback_function(int c_type, reg_callback cb)
{
    int success = -1;
    switch(c_type)
    {
    case 000000:
        printf("registering fixed update\n");
        break;
    default:
        break;
    }
    return success;
}

int register_fixed_update(reg_callback cb)     {
    return register_callback_function(000000, cb);
};

int fixed_update(void *a, void *b) {
}

int main()
{
    register_fixed_update(fixed_update);

    return 0;
}

关于c - 在c中设置一系列函数指针和回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32000126/

相关文章:

c++ - 函数指针在 C++ 中返回另一个具有局部性的函数指针

c++ - 如何在 C++ 中使用模板仿函数实现回调

python - C代码嵌入python回调函数

javascript - 是否可以将参数传递给链式匿名函数回调?

C 将整数转换为字符串

c - 如何搜索返回值位置和比较次数的数组?

dictionary - 用new()初始化map,然后给panic异常赋值,为什么?

php - PHP与C变量作用域差异: block scope is not exactly the same?

c - C 程序设计语言 K&R 练习 1- 9

c - 结构类型数组,C错误