“This is the seventh day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”
1 structure
- It is a construct type, and the members in the structure are contiguously contiguously in memory, but the types of the members in the structure may or may not be the same.
- Define format:
struct student{
char name[20];int age;
char sex;
};
Copy the code
- Define variables using structures
struct student p1,p3; // int p1;
struct student *p2 = &p1; // int *p2 = &p1;
- Assign a value to a member of a structure variable
strcpy(p1.name ,"zhangsan");
p1.age = 25;
p1.sex = 'm';
strcpy(p2->name ,"zhangsan");
p2->age = 25;
p2->sex = 'm';
Copy the code
- Struct: is the keyword of a structure
- Student: type name
- {} : The contents inside are the members of the structure
- ; The end of the structure needs to be added;
- Member types: The types of members in the structure can be different, and can be arbitrary
- struct student p1; Struct student p1 (struct student
- . : Access members of a structure through a structure variable
- -> : If the pointer variable is a structure, access the inside members through ->
2 Array of structures
struct student p1; //int p1;
struct student p1[2]. //int p1[2];
p1[0].name
p1[0].age
p1[0].sex
p1[1].name
p1[1].age
p1[1].sex
Copy the code
3. Use of structures
3.1 Define variables while defining structures
struct student{
char name[20];int age;
char sex;
}p1,p2[2],*p3;
Copy the code
☞ defines the variable p1, the array P2 [2], and the pointer p3.
3.2 Unknown structures
struct {
char name[20];int age;
char sex;
}p1,p2[2],*p3;
Copy the code
☞ defines the variable p1, the array P2 [2], and the pointer p3. You cannot use struct types to define other variables.
3.3 Structure combined with typedef
typedef struct student{
char name[20];int age;
char sex;
}stu_t; / / type
Copy the code
☞ stu_t p1. Stu_t *p2; // A pointer to p2 is defined
typedef struct {
char name[20];int age;
char sex;
}stu_t; / / type
Copy the code
☞ stu_t p1. Stu_t *p2; // A pointer to p2 is defined
3.4 Assignment of struct variables
3.4.1 Define first and then assign
struct student{
char name[20];int age;
char sex;
};
struct student p1;
strcpy(p1.name,"zhangsan");
p1.age = 20;
p1.sex = 'm';
Copy the code
3.4.2 Simultaneous assignment of defined values
struct student{
char name[20];int age;
char sex;
};
struct student p1 = {
"zhangsan".20.'m'
};
Copy the code
Rule 3.4.3. Is assigned to the form
struct student{
char name[20];int age;
char sex;
};
struct student p1 = {
.name = "zhangsan",
.sex = 'm'
};
Copy the code
▌ You can specify a member to be assigned, and ignore members that do not want to be assigned
3.4.4 Define types Define variables and assign values
struct student{
char name[20];int age;
char sex;
}p1 = {
"zhangsan".20.'m'
};
Copy the code
3.4.5 Define a type and define a variable andAssign across members
struct student{
char name[20];int age;
char sex;
}p1 = {
.name = "zhangsan",
.sex = 'm'
};
Copy the code
3.5 Assignment of struct arrays
3.5.1 Define and then assign
struct student p1[2]. //int p1[2];
p1[0].name
p1[0].age
p1[0].sex
p1[1].name
p1[1].age
p1[1].sex
Copy the code
3.5.2 Simultaneous assignment of the definition
struct student p1[2] ={{"zhangsan".20.'m'},
{"lisi".30.'w'}};struct student p1[2] = {
{
.name = "zhangsan",
.age = 20,
.sex = 'm'
},
{
.name = "lisi",
.sex = 'w'}};Copy the code
3.5.3 DefinitionsSkip member assignment
struct student p1[3] ={[0] = {
.name = "zhangsan",
.age = 20,
.sex = 'm'},2] = {
.name = "lisi",
.sex = 'w'}};Copy the code
4 Nesting of structures
struct aa{
int a;
int b;
int c;
};
typedef struct{
char name[20];int age;
char sex;
int score;
struct aa t;
}stu_t;
typedef struct{
stu_t stu[30];
int n; // Number of students in the current class
}class_t;
class_t cls;
cls.stu[0].name
cls.stu[0].age
cls.stu[0].sex
cls.stu[0].score
cls.stu[0].t.a = 10;
cls.stu[0].t.b = 10;
cls.stu[0].t.c = 10; . cls.stu[29]
cls.n
Copy the code
Combination of structure and function pointer
The ☞ struct cannot write functions inside, but it can write function Pointers. Cause: The memory size of the function is uncertain.
struct operations{
int (*open)(void);
int (*read)(void);
int (*write)(void);
int (*close)(void);
};
Copy the code
Struct pointer array, struct array pointer
6.1 Array of structure Pointers
typedef struct{
char name[20];int age;
char sex;
int score;
struct aa t;
}stu_t;
stu_t *p[3]; // Array of struct Pointers
p[0] = malloc(sizeof(stu_t));
p[1] = malloc(sizeof(stu_t));
p[2] = malloc(sizeof(stu_t));
Copy the code
6.2 Struct array Pointers
stu_t (*p)[3]; // It is a pointer to a row.
stu_t arr[4] [3];
p = arr;
Copy the code
7 Memory size used by structures
📌 1. If the members inside the structure are less than or equal to 4 bytes, align with the largest member.
2. If there are more than 4 bytes of members in the structure, all members are aligned in four-byte order
3. If there is a char short occupying 4 bytes inside the structure, the second member is always stored from the even position
8 Commons (consortium)
-
Commons: This is a construct type in which all members within the Commons share the number of bytes occupied by the largest member.
-
Define format:
union aa{
char a;
short b;
int c;
double d;
};
Copy the code
- Define variables:
union aa a;
union aa *b;
- Initialization:
a.a = 'c';
或者 a.d = 3.1415926;
b = malloc(sizeof(*b));
B - > d = 3.1415925;
- The role of the union? (Space saving)
struct class{
struct person p[30].
int n;
};
struct person{
char name[20];
int age;
char sex;
union a{
int score;
int salar;
}b;
}
Copy the code