-
Notifications
You must be signed in to change notification settings - Fork 0
/
unions.c
94 lines (78 loc) · 1.54 KB
/
unions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Although used for coercion on more typesafe languages, unions in C have a
* more profound role: They help create both a way to destructure data, and
* create sort-of polymorphism.
*/
#include <stdio.h>
#include <stdint.h>
/* Coercion example: */
union float_internal_rep {
int i32;
float f32;
};
/* Polymorphism example: */
enum typ { INT64, DOUBLE, CHAR8 };
struct poly {
enum typ poly_t;
union {
long i64;
double f64;
char c8[8];
};
};
/* Destructure example */
union data {
struct {
unsigned byte0:8;
unsigned byte1:8;
unsigned byte2:8;
unsigned byte3:8;
};
unsigned bytes;
};
void polymorphic(struct poly *); /* SEE below */
int main() {
/* coercion */
/* XXX: UB:
* C standard only defines accessing the most recently modified union
* field; accessing any other field is undefined but compiler writers agree
* that it's best used for coercion.
*/
union float_internal_rep f;
f.f32 = 1.2345f;
printf("0x%X\n", f.i32); /* XXX: UB */
/* polymorphism */
struct poly t;
t.poly_t = INT64;
t.i64 = 42L;
polymorphic(&t);
t.poly_t = CHAR8;
t.c8[0] = 'H';
t.c8[1] = 'E';
t.c8[2] = 'L';
t.c8[3] = 'L';
t.c8[4] = 'O';
polymorphic(&t);
/* deconstruction */
union data word;
word.bytes = 0xdeadbeef;
word.byte2 = 0xcc;
printf("0x%X\n", word.bytes);
return 0;
}
void
polymorphic(struct poly *S)
{
switch (S->poly_t) {
case INT64:
printf("long! %ld\n", S->i64);
break;
case DOUBLE:
printf("double! %lf\n", S->f64);
break;
case CHAR8:
puts("char!");
puts(S->c8);
break;
}
}