-
Notifications
You must be signed in to change notification settings - Fork 1
/
uniontest.c
65 lines (53 loc) · 1.04 KB
/
uniontest.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
#include <stdio.h>
#include <stdbool.h>
typedef struct{
int x;
float y;
char z;
} mystr;
typedef union{
int x;
float y;
char z;
} myunion;
typedef struct {
char *name;
bool is_robot;
union{
char *personality;
int firmware_version;
};
} character;
void print_character(character* c){
printf("Characters :: %s\n", c->name);
if(c->is_robot){
printf("version :: %d\n", c->firmware_version);
}else{
printf("personality :: %s\n",c->personality);
}
}
int main(){
mystr s;
s.x = 45;
s.y = 9.87;
s.z = 'H';
myunion u;
u.x = 45;
u.y = 9.87;
u.z = 'H';
printf("struct size :: %lu\n", sizeof(mystr));
printf("struct :: {%d, %f, %c}\n", s.x, s.y, s.z);
printf("union size :: %lu\n", sizeof(myunion));
printf("union :: {%d, %f, %c}\n", u.x, u.y, u.z);
character hansolo;
character r2d2;
hansolo.name = "Han Solo";
hansolo.is_robot = false;
hansolo.personality = "Scruffy looking NerfHerder";
r2d2.name = "R2D2";
r2d2.is_robot = true;
r2d2.firmware_version = 42;
print_character(&hansolo);
print_character(&r2d2);
return 0;
}