简体   繁体   English

如何分配函数指针?

[英]How do I assign function pointers?

I have a function int handle_request(server* server, int conn_fd); 我有一个函数int handle_request(server* server, int conn_fd); that takes in 2 arguments. 需要2个参数 How do I assign it to this function pointer? 如何将其分配给该函数指针? void (*func)(void* input, void* output)

I've been trying to do things like 我一直在尝试做类似的事情

void (*func)(void* input, void* output) = handle_request;

and tried these but I always get 并尝试了这些,但我总是得到

warning: initialization from incompatible pointer type [enabled by default] 警告:从不兼容的指针类型初始化[默认启用]

As mentionied in your question if the function prototype returns int, then the pointer must match it. 如您的问题所述,如果函数原型返回int,则指针必须与之匹配。 If the retuzrn type should be void, then the pointer is also to be void. 如果retuzrn类型应该为空,则指针也将为空。

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

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

or 要么

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;
}

Your function pointer should match your function signature. 您的函数指针应与您的函数签名匹配。 This means that you can assign function pointer: 这意味着您可以分配函数指针:

void (*func)(void* input, void* output)

to the function: 功能:

void handle_request(void *input, void *output)

If you try to it your way a warning will be issued. 如果尝试这样做,将发出警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM