Structural body
A struct is a special type that can be packaged with other types as a composite type. In the object-oriented concept, it is a special class.
Use several forms of structure:
First Kind
Define the struct body first, and then define the struct variable.
Define the structure body:
struct point1{ int x; int y;};
Defining struct-Body variables
struct point1 point;
The second Kind
Define an anonymous struct, and then define struct-body variables
struct{ int x; int y;}point2;
Third Kind
Define struct-body variables while defining structs
struct point3{ int x; int y;}point;
Fourth type
Defining structs with typedef
typedefstruct point4{ int x; int y;}t_point;
Then define the structure variables with T_point
point;
struct array
struct student{ int age; char *name; }; struct student ss[10];
struct-Body pointer
struct student *pst; pst = &foo;
struct initialization
There are structural definitions
struct student{ int age; char *name; };
First Kind
struct student foo1 = {11"xiaoming"}; struct student foo2 = {11}
The second Kind
struct student foo3 = {.age = 11};
Third Kind
struct student foo4 = (struct student){11"xiaoming"}; struct student foo5 = (struct11};
struct array Initialization
struct student ss1[10{0}; struct student ss2[10{{}{}{}}; struct student ss3[10{[2] = {}, [3{}}; struct student ss4[10{[2].age = 10, [3].name = "xiaoming"};
accessing struct members
Use the "." return struct member
struct student foo = {11"xiaoming"}; int age = foo.age; *name = foo.name; printf("age is %d, name is %s\n", age, name); 20; "liyong"; printf("age is %d, name is %s\n", foo.age, foo.name);
When using the struct pointer, you can use the arrow operator
*pst; pst = &foo; printf("pst age is: %d and name is %s\n", (*pst).age, (*pst).name); printf("pst age is: %d and name is %s\n", pst->age, pst->name);
C language structures must be known