forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunion_initializer.c
More file actions
47 lines (41 loc) · 979 Bytes
/
union_initializer.c
File metadata and controls
47 lines (41 loc) · 979 Bytes
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
/*
* Union initializers are part of C99 but not C++.
*
* Embed the test union inside a struct because there was an issue with
* that in VS2013 and union-inside-struct is needed by API macros.
*/
#include <stdio.h>
typedef union {
int a;
double b;
const char *c;
} test_union;
typedef struct {
int etype; /* 0=int, 1=double, 2=string */
test_union u;
} test_struct;
static void dump_struct(test_struct *t) {
switch (t->etype) {
case 0:
printf("Integer: %d\n", t->u.a);
break;
case 1:
printf("Double: %lf\n", t->u.b);
break;
case 2:
printf("String: %s\n", t->u.c);
break;
default:
printf("Unknown type: %d\n", t->etype);
}
}
int main(int argc, char *argv[]) {
test_struct v1 = { 0, 123 }; /* default is to initialize first alternative */
test_struct v2 = { 0, { .a = 234 } };
test_struct v3 = { 1, { .b = 123.456 } };
test_struct v4 = { 2, { .c = "foo" } };
dump_struct(&v1);
dump_struct(&v2);
dump_struct(&v3);
dump_struct(&v4);
}