StarShire
StarShire

Reputation: 309

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]

Upvotes: 0

Views: 130

Answers (2)

Devolus
Devolus

Reputation: 22084

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

Upvotes: 1

nikpel7
nikpel7

Reputation: 676

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.

Upvotes: 0

Related Questions