我有一个函数 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/