1 /* 2 * unionvar.c 3 * 4 * Beispiel-Programm union-Variable 5 * 6 * Autor: H.Drachenfels 7 * Erstellt am: 25.2.2015 / 10.11.2017 (C11) 8 */
9
10 #include <stdio.h>
11
12 enum int_or_string {type_int, type_string};
13
14 struct struct_with_union
15 {
16 enum int_or_string u_type;
17
18 union
19 {
20 int i;
21 char *s;
22 };
23 };
24
25 int main(void)
26 {
27 struct struct_with_union x;
28
29 //-------------------------------------------- print variable value
30 x.u_type = type_int;
31 x.i = 1;
32 printf("%d: %d\n", x.u_type, x.i);
33
34 x.u_type = type_string;
35 x.s = "Hallo";
36 printf("%d: %s\n", x.u_type, x.s);
37
38 //------------------------------------------ print variable address
39 printf("&x = %p\n", (void*) &x);
40 printf("&x.u_type = %p\n", (void*) &x.u_type);
41 printf("&x.i = %p\n", (void*) &x.i);
42 printf("&x.s = %p\n", (void*) &x.s);
43
44 //--------------------------------------------- print variable size
45 printf("sizeof d = %zu\n", sizeof x);
46
47 return 0;
48 }
49