Today, let's talk about structure
Why structure
Because in practical problems, a group of data often has many different data types. For example, to register student information, you may need to use char type name, int type or char type student number, int type age, char type gender, and float type grade. For another example, for recording a book, you need a book title of char type, an author name of char type, and a price of float type. In these cases, it is difficult to use simple basic data types or even arrays. The structure (similar to the "record" in Pascal) can effectively solve this problem.
Structure is essentially a data type, but it can include several "members". The type of each member can be the same or different, basic data type or another construction type.
Advantages of structure:
The structure can not only record different types of data, but also make the data structure "high cohesion and low coupling", which is more conducive to the reading, understanding and transplantation of programs. Moreover, the storage mode of the structure can improve the access speed of CPU to memory.
1, Declaration of structure type
Structure declaration It is the main method to describe how structures are combined.
The general form is:
struct structure name{
Member list
};
The struct keyword indicates that next is a structure.
For example, state the structure of a student:
struct Student{ //Declaration structure char name[20]; //full name int num; //Student number float score; //achievement };
The above declaration describes a structure containing three different types of members, but it has not yet created an actual data object.
Each member variable is described with its own declaration, ending with a semicolon.
A semicolon after curly braces indicates the end of the structure declaration.
The structure declaration can be placed outside the function (at this time, it is a global structure, similar to a global variable, and all functions declared after it can be used)
It can also be placed in a function (this is a local structure, similar to a local variable, which can only be used in this function. If it has the same name as the global structure, the global structure will be temporarily shielded).
Type of structure member It can be variables, arrays, pointers, or even other structures
2, Definition and initialization of structure variables
Definition of structure variables
To define structural variables
The general form is:
struct structure name structure variable name;
For example:
struct Student stu1; //Define structure variables
1. The definition of structure variable can be placed after the declaration of structure:
truct Student //Declaration structure { char name[20]; //full name int num; //Student number float score; //achievement }; struct Student stu1; //Define structure variables
2. The definition of structure variable can also be the same as the declaration of structure, which simplifies the code
struct Student { char name[20]; int num; float score; }stu1; //Variable name after definition
3. You can also use anonymous structs to define struct variables:
struct //No structure name { char name[20]; int num; float score; }stu1;
However, it should be noted that although this method is simple, it can not define new structure variables again
Initialization of structure variables
1. Define variables and assign initial values at the same time
struct Point { int x; int y; }p1; //Define the variable p1 while declaring the type struct Point p2; //Define structure variable p2 //Initialization: define variables and assign initial values at the same time. struct Point p3 = {x, y};
two Assign an overall value to the structure
struct Stu //Type declaration { char name[15];//name int age; //Age }; struct Stu s = {"zhangsan", 20};//initialization
three You can assign values to the members of the structure one by one:
struct Student stu1, stu2; //Define structure variables strcpy(stu1.name, "Jack"); stu1.num = 18; stu1.score = 90.5;
Note: the array name cannot be assigned directly, because the array name is a constant. For example:
stu1.name = "Jack"; //...main.c:26:15: Array type 'char [20]' is not assignable
4. Structure nesting initialization
struct Node { int data; struct Point p; struct Node* next; }n1 = {10, {4,5}, NULL}; //Structure nesting initialization struct Node n2 = {20, {5, 6}, NULL};//Structure nesting initialization
3, Structure member access
Structural variables access members. Members of structural variables are accessed through the point operator (.). The point operator accepts two operands. For example:
We can see that s has member name and age
How do we access members of s?
struct S s; strcpy(s.name, "zhangsan");//Use. To access the name member s.age = 20;//Use. To access age members
Structure pointers access members that point to variables
Sometimes what we get is not a structure variable, but a pointer to a structure.
struct Stu { char name[20]; int age; }; void print(struct Stu* ps) { printf("name = %s age = %d\n", (*ps).name, (*ps).age); //Use structure pointers to access members that point to objects printf("name = %s age = %d\n", ps->name, ps->age); } int main() { struct Stu s = {"zhangsan", 20}; print(&s);//Structure address transfer parameter return 0; }
4, Structural transmission parameters
struct S { int data[1000]; int num; }; struct S s = {{1,2,3,4}, 1000}; //Structural transmission parameters void print1(struct S s) { printf("%d\n", s.num); } //Structure address transfer parameter void print2(struct S* ps) { printf("%d\n", ps->num); } int main() { print1(s); //Transmission structure print2(&s); //Transmission address return 0; }
Which of the print1 and print2 functions above is better?
The answer is: the print2 function is preferred.
reason:
When a function passes parameters, the parameters need to be pressed on the stack.
If the structure is too large when passing a structure object, the system overhead of parameter stack pressing is relatively large, which will lead to performance degradation
Conclusion: when the structure passes parameters, the address of the structure should be passed.