fork download
  1. #include <stdio.h>
  2. #include <stddef.h>
  3.  
  4. typedef struct Node {
  5. int a;
  6. int b;
  7. int c;
  8. } Node;
  9.  
  10. #define SET_MEMBER(obj, member_name, value)\
  11. set_member(value, (void*)((char *)obj + offsetof(Node, member_name)))\
  12. /**/
  13.  
  14. void set_member(int value, int *pointer){//or use memcpy
  15. *pointer = value;
  16. }
  17.  
  18. static inline void print_Node(Node *node);
  19.  
  20. int main(void){
  21. Node mynode = {.a = 0, .b = 45, .c = 64};
  22.  
  23. print_Node(&mynode);
  24. SET_MEMBER(&mynode, a, 1);
  25. print_Node(&mynode);
  26. }
  27.  
  28. static inline void print_Node(Node *node){
  29. printf("{ a:%d, b:%d, c:%d }\n", node->a, node->b, node->c);
  30. }
  31.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
{ a:0, b:45, c:64 }
{ a:1, b:45, c:64 }