fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4. int x, y;
  5. void(*show)();
  6. } point_t;
  7.  
  8. point_t *global_point_ptr;
  9.  
  10. void point_show() {
  11. point_t point = *global_point_ptr;
  12. printf("x = %d, y = %d\n", point.x, point.y);
  13. }
  14.  
  15. point_t point(int x, int y) {
  16. point_t r = { x, y, point_show };
  17. return r;
  18. }
  19.  
  20. point_t *use_point(point_t *p) {
  21. global_point_ptr = p;
  22. return p;
  23. }
  24.  
  25.  
  26.  
  27. int main(void) {
  28. point_t a = point(2, 10);
  29. point_t b = point(-10, 5);
  30.  
  31. use_point(&a)->show();
  32. use_point(&b)->show();
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 4356KB
stdin
Standard input is empty
stdout
x = 2, y = 10
x = -10, y = 5