Mawg Mawg - 1 year ago 90
C Question

How can I store a function pointer in a structure?

I have declared

typedef void (*DoRunTimeChecks)();

How do I store that as a field in a struct? How do I assign it? How do I call the fn()?

Answer Source

Just put it in like you would any other field:

struct example {
   int x;
   DoRunTimeChecks y;

void Function(void)

struct example anExample = { 12, Function };

To assign to the field:

anExample.y = Function;

To call the function:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download