Mawg Mawg - 1 month ago 9
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

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:

anExample.y();
Comments