For example, I have to create a struct in a function with the parameters: a function pointer with the format: void (*func)(void *) and an int id.
The struct to create is the following:
typedef struct example {
int id;
void (*func)(void *);
} Example;
Now my question is when I malloc space for the whole struct, do I also malloc space for the void function pointer because it is a pointer? If so, how, since I can't get the size of a void pointer? Thanks.
When you malloc space for a struct, sizeof(your_struct)
gets you the size of the entire struct. You don't need to individually allocate space for each element. After mallocing, you can set the function pointer like any other field of the struct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With