C-从“ void *”无效转换为“ void(*)()”

时间:2018-08-26 04:25:00

标签: c linux g++ void

当我尝试编译程序时,我得到以下信息:

invalid conversion from ‘void*’ to ‘void (*)()’

initializing argument 2 of ‘void makecontext(ucontext_t*, void (*)(), int, ...)’ [-fpermissive]

invalid conversion from ‘void (*)()’ to ‘void*’ [-fpermissive] init_TCB(tcb,function,stack,STACK_SIZE);
initializing argument 2 of ‘void init_TCB(TCB_t*, void*, void*, int)’ [-fpermissive]
void init_TCB (TCB_t *tcb, void *function, void *stackP, int stack_size)`

以下是导致错误的代码行:

#include <ucontext.h>

typedef struct TCB_t {
   struct TCB_t     *next;
   struct TCB_t     *prev;
   ucontext_t      context;
} TCB_t; 

void init_TCB (TCB_t *tcb, void *function, void *stackP, int stack_size)
{
   memset(tcb, '\0', sizeof(TCB_t));       
   getcontext(&tcb->context);      
   tcb->context.uc_stack.ss_sp = stackP;
   tcb->context.uc_stack.ss_size = (size_t) stack_size;
   makecontext(&tcb->context, function, 0);
}

我想念什么?

1 个答案:

答案 0 :(得分:3)

您有void *function,但是void*不是正确的函数指针类型。 void (*)()是一种函数指针,因此您可能应该更改函数签名:

void init_TCB (TCB_t *tcb, void (*function)(), void *stackP, int stack_size)