>>109631189
Sometimes you want to associate functions with your structs, something like this:
#include <stdlib.h>
typedef struct {
char* text;
int x;
int y;
int width;
int height;
void (*callback)(void);
} Button;
Button* CreateButton(char* text, int x, int y, int width, int height, void (*callback)(void)) {
Button* button = malloc(sizeof(Button));
button->text = text;
button->x = x;
button->y = y;
button->width = width;
button->height = height;
button->callback = callback;
return button;
}
void foo() {
// ...
}
void bar() {
// ...
}
int main() {
Button* foo_button = CreateButton("Foo", 0, 0, 100, 50, &foo);
Button* bar_button = CreateButton("Bar", 0, 200, 100, 50, &bar);
foo_button->callback(); // This will call foo();
bar_button->callback(); // This will call bar();
return 0;
}