我有以下代码:
/* 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) { /* ... */ }
我虽然void *被认为是通用指针。因此,无论我给出的任何地址都不会产生警告,但上述情况action0_t*
与void*
不同,因此产生以下警告:initialization from incompatible pointer type
如果我想抑制警告,我唯一的解决办法是将ProcessMsgAction参数更改为void *?
使用的编译选项:-ansi -pedantic -Wall -Wextra
答案 0 :(得分:2)
I though void* was considered as generic pointer
这是事实,但这个概念并未扩展到函数类型中的匹配参数。
Callback_t
的简单类型转换有效,see this functional example on ideone.