Skip to content

Instantly share code, notes, and snippets.

@Mroik
Last active January 27, 2022 00:10
Show Gist options
  • Save Mroik/4abc8ad1788555e4a00d06ab87494af7 to your computer and use it in GitHub Desktop.
Save Mroik/4abc8ad1788555e4a00d06ab87494af7 to your computer and use it in GitHub Desktop.
This is an example of how to program with OOP paradigm in C (why would you ever do this)
#include <stdio.h>
#include <stdlib.h>
typedef int(*int_ptr)(test);
typedef struct {
int a;
int_ptr next;
} test;
int next_r(test* self)
{
return self->a + 1;
}
test* test_maker()
{
test* a = malloc(sizeof(test));
a->next = &next_r;
return a;
}
int main()
{
test* ss = test_maker();
ss->a = 1;
printf("%d\n", ss->next(ss)); // Prints 2
free(ss);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment