fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stddef.h>
  4.  
  5. typedef struct __attribute__((packed))
  6. {
  7. char a;
  8. short b;
  9. int c;
  10. } my_struct;
  11.  
  12. char buff[50];
  13.  
  14. int main(void) {
  15. my_struct s;
  16. s.a = 1;
  17. s.b = 2;
  18. s.c = 3;
  19.  
  20. memcpy(buff, &s, sizeof(s));
  21.  
  22. short value = 0;
  23. memcpy(&value, buff + offsetof(my_struct, b), sizeof(value));
  24.  
  25. if (value == 2)
  26. {
  27. value = 10;
  28. memcpy(buff + offsetof(my_struct, b), &value, sizeof(value));
  29. }
  30.  
  31. memcpy(&s, buff, sizeof(s));
  32.  
  33. printf("%d\r\n", s.b);
  34.  
  35. // This is a no-no?
  36. my_struct *ptr = (my_struct *)buff;
  37. ptr->b = 1337;
  38. memcpy(&s, buff, sizeof(s));
  39. printf("%d\r\n", s.b);
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 5456KB
stdin
Standard input is empty
stdout
10
1337