CPlay/callbacks.c
2025-08-18 14:59:16 +02:00

35 lines
566 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;
}