fork(1) download
  1. #include <stdio.h>
  2.  
  3. struct obj_s {
  4. unsigned char a;
  5. int b;
  6. const char *c;
  7. };
  8.  
  9. int checkByMode(struct obj_s *self, const char *message) {
  10. return printf("%p: %s\n", self, message);
  11. }
  12.  
  13. #define OBJ_GETTER(member, type) \
  14. type get_##member(struct obj_s *self) { \
  15. int error_code = checkByMode(self, "common_get_"#member); \
  16. return (error_code < 0) ? (type)error_code : self->member; \
  17. }
  18.  
  19. OBJ_GETTER(a, unsigned char);
  20. OBJ_GETTER(b, int);
  21. OBJ_GETTER(c, const char *);
  22.  
  23. int main(void) {
  24. struct obj_s obj = {
  25. .a = 0xff,
  26. .b = 5566,
  27. .c = "Hello, world!"
  28. };
  29. printf("a:%x b:%d c:%s\n", get_a(&obj), get_b(&obj), get_c(&obj));
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
0x7ffdd29e2030: common_get_c
0x7ffdd29e2030: common_get_b
0x7ffdd29e2030: common_get_a
a:ff b:5566 c:Hello, world!