I. Access to Structures1. Direct access to struct members, as follows:
struct a{
int A;
Long *b;
Char c[20];
};
struct A com;
struct member through operator "." Access, the result of the expression COM.A is the array name, which can be used wherever the array name can be used, com.a[4], which will be selected as an element.
2. Indirect access to struct members
struct A *p;
You can use (*p). A to access struct members, but this form is a little less concise so use the operator "-" to access struct members, the arrow operator performs indirect access to the left operand to get the structure pointed to by the pointer, and then accesses a member according to the right-hand operand, p->a.
Ii. Self-reference of the structural body
struct b{
int A;
struct b b;
int C;
};
This reference is not legal, because B is a complete structure, the second member is another complete structure, and also includes her own members, which can be recycled to the size of the structure.
struct b{
int A;
struct B *b;
int C;
};
This declaration is legal and B is now a pointer to the number of bytes it is known to calculate the size of the struct, and this self-reference is legal.
III. structure, pointers and members
typedef struct{
int A;
Short b[2];
}ex1;
typedef struct{
int A;
Char b[3];
Ex1 C;
struct EX1 D;
}ex2;
Ex2 x={1, "My", {2,{3,4}},0};
EX2 *p=&x;
The Tathagata represents this structure:
Create a pointer to an integral type: int *p1; To make it point to integer member A, use & to get a pointer to P->a: P1=&p->a.
Access to nested structures: P->C.A is the access to shaping a in the C struct.
Access Pointer members: Define another structure: Ex y; x.d=&y; The ex->d->c.b[1]=4; is like space:
Four, the structure of the memory alignment
Alignment rules
The compiler on each particular platform has its own default "snap factor", or you can change this factor by precompiling the command #pragma pack (n), where n is the "alignment factor" you want to specify. Rules: 1, data member alignment rules: struct data members, the first data member is placed at offset 0, the subsequent alignment of each data member according to the value specified by #pragma pack and the length of the data member itself, the smaller one. (That is, the offset address of the member after the first data member is the value specified by the #pragma pack and the number of integers in the data member's own length, which is greater than the smaller one) 2, the overall alignment rule of the struct: After the data members have completed their respective alignments, the structure itself is aligned, and the alignment will follow the # The pragma pack specifies the number of values and the maximum data member length of the struct, which is smaller than the other. 3, when the n value of the #pragma pack equals or exceeds the length of all data members, the size of the N value will have no effect. When the structure contains another structure, the contents of the structure can be substituted directly, and the total storage space is calculated.
Example: (int occupies 4 bytes on this platform)
Although A and A1 contain the same members, but a takes up 12 bytes and A1 occupies 8 bytes, the arrangement of the list of structs in the declaration occurs first (the data members appear first with a large self-growth degree).
Basic knowledge of structure and storage allocation