fork download
  1. //main.c
  2. //Why does y get set to 0 in the case of 'Breaks'?
  3. //Compile with gcc -fms-extensions main.c
  4.  
  5. #include <stdio.h>
  6.  
  7. struct Base {
  8. int x;
  9. int y;
  10. };
  11.  
  12. struct Extend {
  13. union {
  14. int X;
  15. struct Base bb;
  16. } uu;
  17. };
  18.  
  19. struct AlsoExtend {
  20. struct Base bb;
  21. int z;
  22. };
  23.  
  24. static struct Extend Works = {
  25. .uu.bb.x = 5,
  26. .uu.bb.y = 3,
  27. //.X = 2
  28. };
  29.  
  30. static struct Extend Breaks = {
  31. .uu.bb.x = 5,
  32. .uu.bb.y = 3,
  33. .uu.X = 2
  34. };
  35.  
  36. static struct AlsoExtend AlsoWorks = {
  37. .bb.x = 5,
  38. .bb.y = 3,
  39. .z = 69
  40. };
  41.  
  42. int main(int argc, char** argv) {
  43.  
  44. printf("Works: x:%d y:%d X:%d\n", Works.uu.bb.x, Works.uu.bb.y, Works.uu.X);
  45. printf("Breaks: x:%d y:%d X:%d\n", Breaks.uu.bb.x, Breaks.uu.bb.y, Breaks.uu.X);
  46. printf("AlsoWorks: x:%d y:%d z:%d\n", AlsoWorks.bb.x, AlsoWorks.bb.y, AlsoWorks.z);
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
Works: x:5 y:3 X:5
Breaks: x:2 y:0 X:2
AlsoWorks: x:5 y:3 z:69