Integration of structural knowledge points (Part 1)
Structural knowledge points (previous part)
(1) Declaration of structure type
(2) Definition and reference of structure variables
(3) Solution of structure size
(4) Initialization of structure type
(5) Definition and initialization of structure array
So why do you need a structure?
Structure can organize data with the same function and exist together, which is convenient to use. Different variables with internal relations can be organized and encapsulated into a whole, that is, defined as a structure. After that, the structure type can be processed like the basic data type.
1. Declaration of structure type
A structure is a construction type that consists of several members. Its members can be a basic data type or another construction type.
Construction data type: construction data type is defined by construction method according to one or more defined data types. That is, the value of a construction type can be decomposed into several "members" or "elements".
The keyword used when declaring the structure is struct, as follows:
/*The keyword is struct*/ struct Student /*Declaration structure*/ { char Name[20]; int Number; char xy; int Grade; }; //Notice the semicolon after the closing brace /*Four different variables are defined in the structure*/
Namely
struct Structure name { member };
2. Definition and reference of structure variables
<1> There are two ways to define structure variables
(1) Declare the structure type before defining the variable.
It should be mentioned that when declaring a structure type, the system will not allocate storage space to it. The system will allocate storage space to it only when defining a structure variable.
struct Student /*Declaration structure*/ { char Name; /*Define structure members*/ int number; float num; double grade; char xy; }; struct Student one;/*Define structure type variables*/ struct Student two;
(2) Structure variables are defined when the structure type is declared.
struct Student //Student is a struct type { char Name; int number; float num; double grade; char xy; };one,two; /*Define structure variables*/
Structure types and variables are different. Student is the structure type, while one and two are the structure variables. You can only assign values to variables, and you can't assign values to types.
Members of a structure can also be variables of structure type. For example:
struct shape /*Declare structure type*/ { int hight; //Including height, weight and hairstyle int weight; char hair; }; struct Student { char Name; int number; float num; double grade; char xy; struct shape boy; //Structure members contain structure type variables }one,two;
Include the struct shape structure type in the struct Student structure type;
<2> . reference of structure variable
After the structure type variable is defined, it can be referenced in the format of "structure variable name. Member name".
For example:
struct Student { char Name[20]; int Number; char xy[20]; int Grade; }one; one.Name = "caigiegie";/*quote*/ one.Grade = 1;
Operations such as assigning values to structure variables are operations on structure members. The above assignment operation is the operation on member Name and Grade.
Note: structural variables cannot be directly input and output as a whole.
For example:
pirntf("%s%d%s%d",one); //This is wrong
The operator '.' is required.
printf("%s",one.Name); printf("%d",one.Number); printf("%s",one.xy); printf("%d",one.Grade);
Members of structure variables can operate like ordinary variables.
For example:
one.Number++;
Because the operator of "." has the highest priority, one.Number + + is a member for self addition.
3. Solution of structure size
In point 2, we mentioned that the storage space will not be allocated when declaring the structure type. The storage space will be allocated only when defining the structure variable. How much memory space is allocated to the structure? So how to solve it?
Look at the following code
struct Student { char Name; //1 bytes char xy; // 1 bytes int number; // 4 bytes float num; // 4 bytes double grade; // 8 bytes }; struct Student one; int main() { int a = sizeof(one.Name ); int b = sizeof(one.xy ); int c = sizeof(one.number); int d = sizeof(one.num ); int e = sizeof(one.grade); int m = sizeof(one); printf("%d %d %d %d %d\n",a,b,c,d,e); printf("%d",m); }
Add up the size of each variable type in the structure?
Is it 1 + 4 + 4 + 8 + 1 = 18?
After running, it is found that simply adding up each memory unit of the structure members is the size of the structure, and simply changing the order of the structure members will result in different results.
struct Student { char Name; // 1 bytes int number; // 4 bytes float num; // 4 bytes double grade; // 8 bytes char xy; // 1 bytes };
So why is it different?
Knowledge of the offset required to calculate the size of the structure:
Offset refers to the difference between the address of the member in the structure variable and the address of the structure variable. The offset of the first member is 0, the offset of the second member is the offset of the first member plus the size of the first member, and so on.
There are two situations to calculate the size of the structure:
<1> : no structure nesting;
Method I:
The size of the structure is the offset of the last member + the number of bytes it occupies. In addition to this criterion, the following two principles need to be met:
1: The offset of a member in a structure must be an integer multiple of the number of bytes in which the member is located.
2: The size of the structure must be an integral multiple of all members, that is, the least common multiple.
struct Student { char Name; //1 bytes char xy; // 1 bytes int number;// 4 bytes float num; // 4 bytes double grade; // 8 bytes };
member | Bytes occupied | Offset |
---|---|---|
Name | 1 | 0 |
xy | 1 | 1 + 0 = 1 (multiple of bytes) |
number | 4 | 1 + 1 = 2 – > 4 (complement to a multiple of the number of bytes) |
num | 4 | 4 + 4 = 8 (multiple of 4) |
grade | 8 | 4 + 8 = 12 – > 16 (make up to a multiple of 8) |
The size of the structure is the byte size of the last member + the offset size
That is, 8 + 16 = 24 (at this time, we need to see whether the second principle is satisfied)
24 is exactly a multiple of all members. It is satisfied and does not need to be supplemented.
So the final structure size is 24.
Let's look at how to solve the size of the structure in the exchange order
struct Student { char Name; // 1 bytes int number;// 4 bytes float num;// 4 bytes double grade; // 8 bytes char xy;// 1 bytes };
member | Bytes occupied | Offset |
---|---|---|
Name | 1 | 0 |
number | 4 | 1+0=1–>4 |
num | 4 | 4+4=8 |
grade | 8 | 4+8=12–>16 |
xy | 1 | 16+8=24 |
The size of the structure is 24 + 1 = 25 – > 32 (if the second principle is not met, supplement to 32)
Method 2: (illustration)
(1) First, find the largest number of bytes in the structure member
(2) Take the byte size as the unit and fill in each member from top to next. When the first unit appears, filling in another member will exceed the unit. Move this member to the next unit and fill in it in turn.
(3) Structure size is n * cell size (n is several cells)
For example:
struct Student { char Name; //1 bytes char xy; // 1 bytes int number;// 4 bytes float num; // 4 bytes double grade; // 8 bytes };
struct Student { char Name; // 1 bytes int number;// 4 bytes float num;// 4 bytes double grade; // 8 bytes char xy;// 1 bytes };
<2> : nested structure:
The nested structure needs to be expanded, and the offset of the first member variable of the expanded structure must be an integer multiple of the largest byte member in the expanded structure.
And follow the above two principles
1: The offset of a member in a structure must be an integer multiple of the number of bytes in which the member is located.
2: The size of the structure must be an integral multiple of all members, that is, the least common multiple.
struct Student { char Name; //1 bytes char xy;//1 bytes struct m { int a; // 4 bytes char b; // 1 bytes }; int number;// 4 bytes float num; // 4 bytes double grade; // 8 bytes }one; int main() { printf("%d",sizeof(one)); }
member | Bytes occupied | Offset |
---|---|---|
Name | 1 | 0 |
xy | 1 | 1+0=1 |
a | 4 | 4 |
b | 1 | 4+4=8 |
number | 4 | 1+8=9–>12 |
num | 4 | 12+4=16 |
grade | 8 | 16+4=20–>24 |
The structure size is 8 + 24 = 32 (condition 2 is met)
So the size of the structure is 32
4. Initialization of structure type
There are also two ways to initialize structure types
(1) Specify an initial value when defining a structure variable.
For example:
struct Student { char Name[20]; char xy[10]; int Grade; }one = {"Caigiegie","male",1}; /*Define structure variables and set initial values*/
(2) Subsequent definition of structure variable re assignment
struct Student { char Name[20]; char xy[10]; int Grade; }; int main() { struct Student one = {"Caigiegie","male",1}; /*Subsequently, structure variables are defined and given initial values*/ printf("full name:%s\n",one.Name ); printf("Gender:%s\n",one.xy ); printf("grade:%d\n",one.Grade ); }
5. Definition and initialization of structure array
<1> . definition of structure array
Defining a structure array is the same as defining a structure variable.
For example:
struct Structure name { member; }Array name;
For example, define a structure array that contains information about 5 students.
struct Student { char Name[20]; int age; int Grade; char xy[20]; }one[5];//Structure array is defined when structure type is defined
Or declare the structure type first and then define the structure array
struct Student one[5];
<2> . initialization of structure array
The form of initializing structure array is as follows:
struct Structure name { Member list; }Array name = {initialize value};
For example:
struct Student { char Name[20]; int age; int Grade; char xy[20]; }one[3] = {{"c",15,1,"male"},{"d",16,2,"female"}, {"c",17,3,"male"}}; /*Define the array and initialize*/
About outputting structure arrays:
It is output using a loop
int i; for(i=0; i<3; i++) //Define loop variable i { printf("%s %d %d %s\n",one[i].Name,one[i].age,one[i].Grade,one[i].xy); }
End of previous knowledge points--------