Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- typedef struct _foo {
- union {
- char c;
- int i;
- } data;
- void (*print)(struct _foo *);
- } foo;
- static void print_char(foo *bar)
- {
- printf( "data.char: %cn", bar->data.c );
- }
- static void print_int(foo *bar)
- {
- printf( "data.int : %dn", bar->data.i );
- }
- int main(void)
- {
- foo int_foo = { .data.i = 1, .print = print_int };
- foo char_foo = { .data.c = 'a', .print = print_char };
- foo *foo_ptr;
- foo_ptr = &char_foo;
- foo_ptr->print(foo_ptr);
- foo_ptr = &int_foo;
- foo_ptr->print(foo_ptr);
- return 0;
- }
Add Comment
Please, Sign In to add comment