这个问题是关于线程的,但我有一个更简单的案例。(初学者)
我在不同的 C++ 编译器中尝试了代码,但不起作用。
请告知如何替换该行: callback = (void*)myfunc;
//-->error
typedef struct _MyMsg {
int appId;
char msgbody[32];
} MyMsg;
void myfunc(MyMsg *msg)
{
if (strlen(msg->msgbody) > 0 )
printf("App Id = %d \n Msg = %s \n",msg->appId, msg->msgbody);
else
printf("App Id = %d \n Msg = No Msg\n",msg->appId);
}
/*
* Prototype declaration
*/
void (*callback)(void *);
int main(void)
{
MyMsg msg1;
msg1.appId = 100;
strcpy(msg1.msgbody, "This is a test\n");
/*
* Assign the address of the function 'myfunc' to the function
* pointer 'callback'
*/
//line throws error: invalid conversion from void* to void(*)(void*)[-fpermissive]
callback = (void*)myfunc; //--> error
/*
* Call the function
*/
callback((MyMsg*)&msg1);
return 0;
}