RicardasSim

union c

Sep 24th, 2018
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. //-std=c99
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main (int argc, char **argv)
  6. {
  7.  
  8.     typedef struct s1 {
  9.         float fV1;
  10.         float fV2;
  11.     } s1;
  12.  
  13.     typedef struct s2 {
  14.         float fV1;
  15.         char cV2;
  16.     } s2;
  17.  
  18.     typedef union u1 {
  19.         s1 m1;
  20.         s2 m2;
  21.     } u1;
  22.  
  23.     union u1 nu1 = {{0.1f,0.2f}};
  24.  
  25.     printf("nu1 (0): %f\n",nu1.m1.fV1);    
  26.     printf("nu1 (1): %f\n\n",nu1.m1.fV2);
  27.    
  28.     //designated union initializer
  29.     union u1 nu2 = {.m2.fV1 = 0.3f, .m2.cV2='C'};
  30.    
  31.     printf("nu2 (0): %f\n",nu2.m2.fV1);    
  32.     printf("nu2 (1): %c\n\n",nu2.m2.cV2);
  33.  
  34.     union u1 nu3;
  35.    
  36.     nu3.m2.fV1 = 0.5f;
  37.     nu3.m2.cV2 = 'c';
  38.    
  39.     printf("nu3 (0): %f\n",nu3.m2.fV1);    
  40.     printf("nu3 (1): %c\n\n",nu3.m2.cV2);
  41.  
  42.  
  43.    
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment