Guest User

Untitled

a guest
Jan 11th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. typedef struct _foo {
  4.  
  5. union {
  6. char c;
  7. int i;
  8. } data;
  9.  
  10. void (*print)(struct _foo *);
  11.  
  12. } foo;
  13.  
  14. static void print_char(foo *bar)
  15. {
  16. printf( "data.char: %cn", bar->data.c );
  17. }
  18.  
  19. static void print_int(foo *bar)
  20. {
  21. printf( "data.int : %dn", bar->data.i );
  22. }
  23.  
  24. int main(void)
  25. {
  26. foo int_foo = { .data.i = 1, .print = print_int };
  27. foo char_foo = { .data.c = 'a', .print = print_char };
  28. foo *foo_ptr;
  29.  
  30. foo_ptr = &char_foo;
  31. foo_ptr->print(foo_ptr);
  32.  
  33. foo_ptr = &int_foo;
  34. foo_ptr->print(foo_ptr);
  35.  
  36. return 0;
  37. }
Add Comment
Please, Sign In to add comment