Change from: http://www.cnblogs.com/liangbo-1024/p/9188497.html
1. Definition and declaration of structure
Structures are defined as follows: struct is the key word of the structure, tag is the symbol of the structure, member-list is the list of members of the structure, which must list all its members; variable-list is the variable declared for the structure.
struct tag { member-list } variable-list ;
In general, tag, member-list and variable-list are at least two parts. The following are examples:
//This Statement Declares a three-member structure, integer a, character b, and double-precision c. //At the same time, the structural variable s1 is declared. //This structure does not label it. struct { int a; char b; double c; } s1; //This Statement Declares a three-member structure, integer a, character b, and double-precision c. //The label of the structure is named SIMPLE, and no variables are declared. struct SIMPLE{ int a; char b; double c; }; //Structures labeled with SIMPLE, and variables t1, t2, t3 are also declared struct SIMPLE t1, t2[20], *t3; typedef struct SIMPLE0{ int a; char b; double c; } Simple3; // Simple3 is an alias for SIMPLE0 //You can also use typedef to create new types (aliases) typedef struct{ //Can not label int a; char b; double c; } Simple2; //Simple2 can now be used as a type to declare new structural variables Simple2 u1, u2[20], *u3;
In the above declaration, the first and second declarations are treated by the compiler as two completely different types, even if their membership list is the same, it is illegal to make t3=&s1. The members of a structure can include other structures or pointers to their own structure types, which are usually used to implement more advanced data structures such as linked lists and trees.
If the two structures contain each other, an incomplete declaration of one of them is required, as follows:
//Structure A contains pointers to structure B struct A { struct B *partner; //other members; }; //Structural B contains a pointer to Structural A, which is declared by B after A is declared. struct B { struct A *partner; //other members; };
2. self citation
2.1 When typedef is not used
The wrong way:
struct tag_1 { struct tag_1 A; /* structural morphology */ int value; };
This statement is wrong, because it is an infinite loop, member b is a structure, and there will be members inside b are the structure, in turn, wireless cycle. When allocating memory, it is illegal because it is infinitely nested and cannot determine the length of the structure.
The right way: (using pointers):
struct tag_1 { struct tag_1 *A; /* Pointer */ int value; };
Since the length of the pointer is fixed (on a 32-bit machine, the length of the pointer is 4), the compiler can determine the length of the structure.
1.2 When typedef is used
The wrong way:
typedef struct { int value; NODE *link; /* Although pointers are also used, the problem here is that NODE has not been defined yet. */ } NODE;
The goal here is to use typedef to create an alias NODEP for the structure. But this is wrong, because the scope of the type name starts at the end of the statement and cannot be used inside the structure because it is undefined.
The right way: there are three ways, the difference is not big, use any one can.
/* Method 1 */ typedef struct tag_1{ int value; struct tag_1 *link; } NODE; /* Method two */ struct tag_2; typedef struct tag_2 NODE; struct tag_2{ int value; NODE *link; }; /* Method three */ struct tag_3{ int value; struct tag_3 *link; }; typedef struct tag_3 NODE;
3. Mutual Reference Structures
The wrong way:
typedef struct tag_a{ int value; B *bp; /* Type B is not yet defined */ } A; typedef struct tag_b{ int value; A *ap; } B;
The reason for the error is the same as above, where type B is used before it is defined.
Correct way: (using "incomplete declaration")
/* Method 1 */ struct tag_a{ struct tag_b *bp; /* Strct tag_b is not defined here, but the compiler can accept it. */ int value; }; struct tag_b{ struct tag_a *ap; int value; }; typedef struct tag_a A; typedef struct tag_b B; /* Method two */ struct tag_a; /* incomplete declaration using structure */ struct tag_b; typedef struct tag_a A; typedef struct tag_b B; struct tag_a{ struct tag_b *bp; /* Strct tag_b is not defined here, but the compiler can accept it. */ int value; }; struct tag_b{ struct tag_a *ap; int value; };