简体   繁体   中英

How do I assign function pointers?

I have a function int handle_request(server* server, int conn_fd); that takes in 2 arguments. 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. If the retuzrn type should be void, then the pointer is also to be void.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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