在C中的结构中,是否需要malloc空间用于函数指针?

ass*_*saf 3 c malloc struct pointers dynamic-memory-allocation

例如,我必须在函数中使用以下参数创建结构:一个函数指针,格式为:void(*func)(void*)和一个int id.

要创建的结构如下:

typedef struct example {
    int id;
    void (*func)(void *);
} Example;
Run Code Online (Sandbox Code Playgroud)

现在我的问题是当我为整个结构我的malloc空间时,我是否也为void函数指针的malloc空间,因为它是一个指针?如果是这样,怎么样,因为我无法获得无效指针的大小?谢谢.

use*_*949 5

当你为一个结构的malloc空间时,sizeof(your_struct)获得整个结构的大小.您不需要为每个元素单独分配空间.mallocing之后,您可以像结构的任何其他字段一样设置函数指针.