CPlay/callbacks.c
2025-06-01 14:31:54 +02:00

31 lines
558 B
C

/*
* Callback table, used for abstracting behaviour. Also known as function
* pointer table This is whats caleld a vtable in C++ terms.
*
* Commonly used as a form of polymorphism
*/
#include <stdio.h>
static int global = 0;
typedef struct {
void (*put)(int);
int (*get)();
} Test;
void my_put(int a) { global = a; }
int my_get() { return global; }
void executor(Test *t) {
t->put(10);
int got = t->get();
printf("Got: %d\n", got);
}
int main() {
Test t = {.get = my_get, .put = my_put};
executor(&t);
return 0;
}