struct struct structure body label (optional) {type 1 identifier 1; type 2 identifier 2; ...} (variable definitions are optional);
Union Union optional Label {type 1 identifier 1; type 2 identifier 2; .....
} optional variable definitions; Unions are similar to structs, but there are key differences in memory layout. Each member in the struct is stored sequentially, whereas in a union, all members are stored starting at an offset of 0 (where the position is overlapping), andat some point, only one member is actually stored in the address change.。
Enumeration (it is used to declare a set of named constants, which can be defined as enumerated types when there are several possible values for a variable) enum optional label {content ...} Optional variable definition; If an identifier in the list is assigned, then the next marker is 1 larger than the assigned value. However, for identifiers that are not previously defined, the names in the enumeration are typically always visible in the debugger and can be used while debugging code, while the constants defined by the # define macro are The general compile time is replaced.
Sample Program
[CPP]View Plaincopyprint?
- #include <stdio.h>
- struct s
- {
- int A;
- Char C;
- } S1;
- enum e
- {
- A,b=3
- }e1;
- Union u
- {
- int A;
- int b;
- Char C;
- }U1;
- int Main (void)
- {
- S1.a=3;
- U1.a=3;
- u1.b=4;
- E1=b;
- printf ("struct:s1.a=%d\n", s1.a);
- printf ("enum:e1_b=%d\n", E1);
- printf ("union:u1.a.&=%x\n", &u1.a);
- printf ("union:u1.b.&=%x\n", &u1.b);
- }
Run results
From the running result, we know that all members of the Union share a storage address, and only one exists.
C Basic Knowledge----Union && enumeration && struct