Unions P194 A single object can be referenced by using different data types The syntax of a union declaration is identical to that for structures, but its semantics are very different Rather than having the different fields reference different blocks of memory they all reference the same block syntax:语法 大 semantic:语义
11 Unions P194 • A single object can be referenced by using different data types • The syntax of a union declaration is identical to that for structures, but its semantics are very different • Rather than having the different fields reference different blocks of memory, they all reference the same block *syntax: 语法 *semantic: 语义
Unions struct S3 char ci int主[2]; double v; union U3 i char c int主[2]; doubley 12
12 Unions struct S3 { char c; int i[2]; double v; }; union U3 { char c; int i[2]; double v; };
Unions P195 The offsets of the fields, as well as the total size of data types s3 and U3,are pe C size 53041220 U30 0 0 8
13 Unions P195 • The offsets of the fields, as well as the total size of data types S3 and U3, are: Type c i v size S3 0 4 12 20 U3 0 0 0 8
Unions P195 struct NODE struct NODe*]ef七; struct NoDe *righti double datai }; union NODE struct union NODE *lefti union NODE *right f internal double data }
14 Unions P195 struct NODE { struct NODE *left; struct NODE *right; double data; }; union NODE { struct { union NODE *left; union NODE *right; } internal; double data; };
Unions (additional tag field) struct NoDe int is leaf; union struct s七 ruct Node★1eEt; struct NODE *righti t internal; double data t info; 15
15 Unions (additional tag field) struct NODE { int is_leaf; union { struct { struct NODE *left; struct NODE *right; } internal; double data; } info; };