RicardasSim

Anonymous struct union

Aug 12th, 2026
16,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. //-Wall -Wextra -Wpedantic -Wshadow -Wconversion
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define TEST_A
  6. #define TEST_B
  7.  
  8.  
  9. typedef struct
  10. {
  11.     struct
  12.     {
  13.         union
  14.         {
  15.             unsigned char uc;
  16.             unsigned int ui;
  17.         } TEST_A;
  18.  
  19.         unsigned int val_1;
  20.  
  21.     } TEST_B;
  22.    
  23.     unsigned int val_2;
  24.    
  25. } block_1;
  26.  
  27. typedef struct
  28. {
  29.     struct
  30.     {
  31.         union
  32.         {
  33.             unsigned char uc;
  34.             unsigned int ui;
  35.         } TEST_C;
  36.  
  37.         unsigned int val_1;
  38.  
  39.     } TEST_D;
  40.    
  41.     unsigned int val_2;
  42.    
  43. } block_2;
  44.  
  45. typedef struct
  46. {
  47.     struct
  48.     {
  49.         union
  50.         {
  51.             unsigned char uc;
  52.             unsigned int ui;
  53.         };
  54.  
  55.         unsigned int val_1;
  56.  
  57.     };
  58.    
  59.     unsigned int val_2;
  60.    
  61. } block_3;
  62.  
  63. int main()
  64. {
  65.        
  66.         block_1 b1;
  67.  
  68.         b1.ui = 123;
  69.  
  70.         unsigned int v = b1.ui;
  71.  
  72.         printf("(1) %d\n", v);
  73.  
  74.         block_2 b2;
  75.  
  76.         b2.TEST_D.TEST_C.ui = 456;
  77.  
  78.         v = b2.TEST_D.TEST_C.ui;
  79.  
  80.         printf("(2) %d\n", v);
  81.  
  82.         block_3 b3;
  83.  
  84.         b3.ui = 789;
  85.  
  86.         v = b3.ui;
  87.  
  88.         printf("(3) %d\n", v);
  89.  
  90.     return 0;
  91. }
  92.  
  93. /*
  94. Output:
  95.  
  96. (1) 123
  97. (2) 456
  98. (3) 789
  99.  
  100. */
  101.  
Advertisement
Add Comment
Please, Sign In to add comment