Skip to content

Instantly share code, notes, and snippets.

@s18alg
Last active September 18, 2019 19:30
Show Gist options
  • Save s18alg/38cb59ee502a95dc011c8f9604e24fb7 to your computer and use it in GitHub Desktop.
Save s18alg/38cb59ee502a95dc011c8f9604e24fb7 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
typedef struct s_main {
void (*f)(void);
} t_main;
typedef struct s_c1 {
void (*f)(void);
int padding;
int pad;
} c1;
typedef struct s_c2 {
void (*f)(void);
void (*g)(const char *);
} c2;
void f(){
printf("f called\n");
}
void f2(){
printf("f2 on board\n");
}
void g(const char* s){
printf("g called with %s\n", s);
}
t_main *new_main(){
t_main *ret = malloc(sizeof(t_main));
ret->f = (f);
return ret;
}
t_main *new_c1(){
c1 *ret = malloc(sizeof(c1));
ret->f = (f);
ret->padding = 10;
ret-> pad = 0;
return (t_main*)ret;
}
t_main *new_c2(){
c2 *ret = malloc(sizeof(c2));
ret->f = (f2);
ret->g = (g);
return (t_main*)ret;
}
int main(){
t_main *a, *b, *c;
c1* e;
c2* d;
a = new_main();
b = new_c1();
c = new_c2();
d = (c2*)c;
e = (c1*)b;
a->f();
b->f();
c->f();
d->g("test");
printf("%d\n", e->padding);
return 0;
}
/** stdout result
f called
f called
f2 on board
g called with test
10
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment