c - 如何分配函数指针?

标签 c function pointers function-pointers

我有一个函数 int handle_request(server* server, int conn_fd); ,它接受 2 个参数。 如何将它分配给这个函数指针? void (*func)(void* 输入,void* 输出)

我一直在尝试做类似的事情

void (*func)(void* 输入,void* 输出) = handle_request;

and tried these但我总是得到

warning: initialization from incompatible pointer type [enabled by default]

最佳答案

正如您的问题中提到的,如果函数原型(prototype)返回 int,则指针必须与其匹配。如果 retuzrn 类型应为 void,则指针也应为 void。

void handle_request(void* input, void* output)
{
}

int main(int argc, _TCHAR* argv[])
{
    void (*func)(void* input, void* output) = handle_request;
    return 0;
}

int handle_request(void* input, void* output)
{
    return 0;
}

int main(int argc, _TCHAR* argv[])
{
    int (*func)(void* input, void* output) = handle_request;
    return 0;
}

关于c - 如何分配函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20653769/

相关文章:

c - 如何读取C中的输入

c - 什么时候初始化指针?

c - 获取函数指针以供其他函数使用

c++指针总是在同一个地址,即使没有被删除

C++ 将 char 转换为 const char *

c - sdl ttf windows-1250 编码

javascript 函数说未定义

javascript - 如何使用 JavaScript 动态删除换行符和文本输入?

c++ - 包装的 unique_ptr 的模板化 move 构造函数

c - 从缓冲区读取数据的最佳实践