混淆了如何访问存储在void指针(void*)中的函数指针.
假设你有这个:
void *functions[] =
{
&sqrt, // int ft_sqrt(int nb);
&power,
&logN,
&factorial;
};
// An array of void pointers, each storing a function pointer.
Run Code Online (Sandbox Code Playgroud)
如果我想访问该sqrt功能,我的猜测如下:
(int (*)(int)) functions[0](x)
但我的猜测是错误的:
error: called object type 'void *' is not a function or function pointer
那么如何访问其中一个函数呢?
c pointers function-pointers void-pointers typecasting-operator