Phong
Phong

Reputation: 6768

callback with void pointer argument produce warning

I have the following code:

/* callback taking a pointer to a "generic" msg as argument */
typedef void (*Callback_t)(void *msg);

/* initialize callback pointer */
Callback_t const Callback[] =
{
  ProcessMsgAction0,  /**< 0: MSGID_ACTION_0 */
  ProcessMsgAction1,  /**< 1: MSGID_ACTION_1 */
  /* ... */
};

/* Call the callback according to msgid */
msg = GetMessage();
Callback[msg->id](msg);

/* implement callback for each message id */
void ProcessMsgAction0(action0_t *msg) { /* ... */ }
void ProcessMsgAction1(action1_t *msg) { /* ... */ }

I though void* was considered as generic pointer. So whatever address i give it wont produce a warning but on the case above action0_t* is different of void* so it produce the following warning: initialization from incompatible pointer type

If I want to suppress the warning, is my only solution to change ProcessMsgAction argument to void* ?

compile option used: -ansi -pedantic -Wall -Wextra

Upvotes: 2

Views: 1301

Answers (1)

Alexander Gessler
Alexander Gessler

Reputation: 46607

I though void* was considered as generic pointer

This is true, but the notion does not extend to matching parameters in function types.

A simple typecast to Callback_t works however, see this functional example on ideone.

Upvotes: 2

Related Questions