OIQ

Untitled

OIQ
May 12th, 2021
590
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4.  
  5. enum Types {
  6.     intType, doubleType, charType, empty
  7. };
  8.  
  9. struct Tagged{
  10.     enum Types types;
  11.  
  12.     union {
  13.         int a;      // if intType
  14.         double b;   // if doubleType
  15.         char c;     // if charType
  16.     } value;        // if empty
  17. };
  18.  
  19. void print(struct Tagged *data) {
  20.     for (struct Tagged *st = data; st < data + 5; st++) {
  21.         switch (st->types) {
  22.         case intType: printf("union containes int value: %d\n", st->value.a); break;
  23.         case doubleType: printf("union containes double value: %f\n", st->value.b); break;
  24.         case charType: printf("union containes char value: %c\n", st->value.c); break;
  25.         case empty: printf("union contains nothing\n"); break;
  26.         default: break;
  27.         }
  28.     }
  29. }
  30.  
  31. int main() {
  32.     struct Tagged data[5];
  33.  
  34.  
  35.     data[0].types = 3;
  36.  
  37.     data[1].types = 3;
  38.  
  39.     data[2].types = 0;
  40.     data[2].value.a = 5;
  41.  
  42.     data[3].types = 2;
  43.     data[3].value.c = 'i';
  44.  
  45.     data[4].types = 3;
  46.  
  47.     print(&data);
  48.  
  49.     return 0;
  50. }
RAW Paste Data