Search notes:

C: union

#include <stdio.h>
#include <string.h>

#define type_i 1
#define type_c 2
#define type_d 3

struct Nums {
  int a;
  int b;
};

struct Text {
  char c8[8];
};

struct Dbl {
  double d;
};

struct U {

  int type;
  union {
    struct Nums nums;
    struct Text text;
    struct Dbl  dbl ;
  } u;

};

void show_u(struct U* u) {

  if      (u->type == type_i) {

           printf("type_i: a      = %d, b = %d\n", u->u.nums.a, u->u.nums.b);

  }
  else if (u->type == type_c) {

           printf("type_c: text   = %s\n", u->u.text.c8);

  }
  else if (u->type == type_d) {

           printf("type_d: double = %7.5f\n", u->u.dbl.d);

  }

}

int main() {

  struct U u1, u2, u3;

  u1.type=type_i;
  u1.u.nums.a = 42;
  u1.u.nums.b = 44;

  u2.type=type_c;
  strcpy(u2.u.text.c8, "1234567");

  u3.type=type_d;
  u3.u.dbl.d = 3.14156;

  show_u(&u1);
  show_u(&u2);
  show_u(&u3);

}
Github repository about-c, path: /union.c

See also

C language

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759398263, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/C-C-plus-plus/C/language/union(107): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78