Structure (Sturct)
C arrays allow you to define variables that can store items of the same data type. Structures are another user-defined data type available in C programming that allows you to store items of different types.
Defining structure
struct tag {
member-list
member-list
member-list
...
} variable-list ;
Copy the code
Tag is a structure tag. Member-list is a standard variable definition, such as int I; Or float f, or some other valid definition variable. Variable-list structural variables, defined at the end of the structure, before the last semicolon, you can specify one or more structural variables. Here’s how to declare the Book structure:
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Copy the code
In general, at least two tags, member-list, and variable-list must appear.
Struct {int a; struct {int a; struct {int a; struct {int a; char b; double c; } s1; Struct SIMPLE {int a; struct SIMPLE {int a; struct SIMPLE {int a; char b; double c; }; Struct SIMPLE t1, t2[20], *t3; struct SIMPLE t1, t2[20]; // It is also possible to create a new type typedef struct {int a; char b; double c; } Simple2; Simple2 u1, u2[20], *u3;Copy the code
In the above declaration, the first and second declarations are treated by the compiler as completely different types, even though their member lists are the same. Making T3 =&s1 is illegal.
The members of a structure can contain other structures or Pointers to its own structure type, and these Pointers are usually used to implement more advanced data structures such as linked lists and trees.
Struct COMPLEX {char string[100]; struct COMPLEX {char string[100]; struct SIMPLE a; }; Struct NODE {char string[100]; struct NODE {char string[100]; struct NODE *next_node; };Copy the code
If two structures contain each other, an incomplete declaration is required for one of them, as follows:
struct B; Struct A {struct B *partner; struct A {struct B *partner; //other members; }; Struct B {struct A *partner; //other members; };Copy the code
Struct sizes follow byte alignment.
Struct alignment rule
A domain
The bit-field definition is similar to the structure definition and has the form:
Struct bit domain struct {bit domain list};Copy the code
Where the bit field list is in the form of:
Type specifier bit domain name: bit field length (how many bits)Copy the code
Such as:
struct bs{
int a:8;
int b:2;
int c:6;
}data;
Copy the code
Data is a BS variable, which contains two bytes. Among them, bit domain A occupies 8 bits, bit domain B occupies 2 bits, and bit domain C occupies 6 bits.
There are a few more notes on the definition of bit-fields:
- Int: 0 indicates that the following types cannot be filled in.
struct area { int a:3; int:0; Int c:20; int c:20; };Copy the code
The structure size is 4+4=8; If there is no int: 0, the size is 4.
- Int: x indicates how many bits are occupied. The occupied bits are not available, but the following bits can be filled in.
struct area { int a:3; int:4; // Indicates that four bits between a and C are unavailable. int c:20; };Copy the code
The structure has a size of 4.
If int:4 is changed to int:10, the structure size becomes 8.
- Bitfields cannot span bytes. Bitfields need to be aligned with the longest type of the structure. The preceding type should be automatically aligned before padding.
struct area1 { char a:6; // Align to int, extend to 32 bits char b:3; int c:22; };Copy the code
Structure size: 8
struct area2 { char a:6; // Align to int, extend to 32 bits char b:3; int c:21; };Copy the code
Structure size: 4
General rules for bit-field compression
- If adjacent bit-field fields are of the same type and the sum of their bitwidths is less than the sizeof the type, the following fields will be stored next to the previous one until it cannot accommodate them.
- If adjacent bit-field fields are of the same type, but the sum of their bitwidths is greater than sizeof the type, the following fields will start from the new storage unit and have an offset that is an integer multiple of their type size.
- If the type of the adjacent bit-field field is different, the specific implementation of each compiler is different. VC6 adopts uncompressed mode, while dev-C ++ adopts compressed mode.
- If bit-field fields are interspersed with non-bit-field fields, no compression is performed.
- The total size of the entire structure is an integer multiple of the size of the widest primitive type member.
Union/Community (Union)
A union has some similarities with a struct. But there are fundamental differences. In a structure, each member has its own memory space, and the total length of a structural variable is the sum of the member lengths. In a union, each member shares a memory space, and the length of a union variable is equal to the longest length of each member.
- A union is a structure;
- All its members have an offset of 0 relative to the base address;
- The structure should be large enough to accommodate the widest members;
- Its alignment should fit all its members;
Union memory alignment
The space occupied by the consortium needs to meet the following conditions:
- Large enough to accommodate the widest members;
- The size is divisible by the size of all the base data types it contains.
union U
{
char s[9]; // 9
int n; // 4
double d; // 8
};
Copy the code
The size of the union is 16.
Reference:
C structure
Union alignment with memory
On the union in C language