Concepts of Structures
Structures are a collection of data of the same or different types. Each data that composes structured data is called a "member" of structured data. Structures are often used to represent several types of data that are related but of different types.
Declaration of structure type
Declarations of structure types are to be used s t r u c t \color{#0000FF}{ struct} Strct keyword, for example, if I want to define a structure type for a student, the student has a name, age, school number, and so on. Then I can declare a student's type of structure like this:
struct stu { char name[20];//Full name int age;//Age char id[15];//School Number };
When I declared the student structure, I wrote only the name, age and number. If you want to define other variables, you can also define them within the structure. Strct stuct is the structure type and cannot be omitted. Members of a structure can be scalars, arrays, pointers, or even other structures.
Creation of Structural Variables
struct stu { char name[20];//Full name int age;//Age char id[15];//School Number }s1,s2,s3;//global variable int main() { //local variable struct stu s4; struct stu s5; return 0; }
Looking at the code above, definitions of structure variables can be created directly when declaring the type of structure, if created directly like s1,s2,s3. So s1,s2,s3 are global variables. Structural variables, such as s4 and s5, can also be created in the main function using the struct stu structure type. Structural variables created in the main function are local variables.
typedef keyword
To add another point of knowledge to you, is the typedef keyword. I believe you have also seen this keyword when you are learning structure or data structure. It is usually used with structure, so we still need to master the typedef keyword.
Typedef replaces the original type name with a new type name. Simply put, typedef is a renamed keyword.
Look at this code:
#include<stdio.h> typedef int data;//Define data as int type int main() { data a = 5; printf("%d", a); return 0; }
I use typedef to change data to int type, so I can use data to create a variable that is an integer type, and the output is 5, no problem.
Not only can data types be renamed using typedef, but also structures. There are two general ways to use the typedef keyword in a structure.
#include<stdio.h> typedef struct stu { char name[20];//Full name int age;//Age char id[15];//School Number }Stu; int main() { struct stu s4; struct stu s5; Stu s6; return 0; }
In this way of writing, the structure is renamed directly, but global variables for the structure cannot be created at this time. Variables can be created in the main function by the structure type or directly by renaming the name. Let's look at another way of writing.
#include<stdio.h> struct stu { char name[20];//Full name int age;//Age char id[15];//School Number }s1,s2,s3; typedef struct stu Stu; int main() { struct stu s4; struct stu s5; Stu s6; return 0; }
You can create global variables like this, but the two methods work in the same way, both **typedef struct stu Stu;** It's just a different location.
Nesting of Structures
Nesting a structure means placing another structure inside a structure. For instance:
struct score { float Chinese; float Math; float Enlish; float ave; }; struct stu { char name[20];//Full name int age;//Age char id[15];//School Number struct score Score; }s1, s2, s3;
I have a score structure nested inside the student structure. The nesting of the structure is very simple, but there is a point to note. Structures are nested sequentially. \color{#0000FF}{The nesting of structures is sequential.} Structures are nested sequentially. Because I am the nested score structure in the student structure, I want to declare the score structure first. If I put it under the student structure, the code will error.
Initialization of Structural Variables
Structures are initialized with {}, and {} is also used inside nested structures. It is very simple to see.
#include<stdio.h> struct score { float Chinese; float Math; float Enlish; }Score1={63.4,56.8,51.3}; struct stu { char name[20];//Full name int age;//Age char id[15];//School Number struct score Score2; }s1, s2, s3; int main() { struct stu s4 = { "Zhang San", 15, "20220805", {63.4,56.8,51.3} }; struct stu s5; return 0; }
It can be initialized directly when a member variable is created, or it can be initialized in the main function.
Access to structure members
Structural members can be accessed in two ways, one is'.', the other is'->'. For instance:
#include<stdio.h> struct score { float Chinese; float Math; float Enlish; }Score1={63.4,56.8,51.3}; struct stu { char name[20];//Full name int age;//Age char id[15];//School Number struct score Score2; }s1, s2, s3; int main() { struct stu s4 = { "Zhang San", 15, "20220805", {63.4,56.8,51.3} }; struct stu s5; printf("%s\n", s4.name); printf("%d\n", s4.age); printf("%.1f\n", s4.Score2.Chinese); return 0; }
If you want to output the value of a member of a structure, you can do so through the structure variable, structure member.
Here's another way to access it, accessing through -> is usually a structure pointer -> structure member.
#include<stdio.h> struct stu { char name[20];//Full name int age;//Age char id[15];//School Number struct score Score2; }s1, s2, s3; void print(struct stu* sp) { printf("%s", sp->name); } int main() { struct stu s4 = { "Zhang San", 15, "20220805", {63.4,56.8,51.3} ,5}; print(&s4);; return 0; }
These contents are not very difficult, you should be able to master them with practice.
Transfer of Structures
Let's start with the following code:
#include<stdio.h> struct stu { char name[20];//Full name int age;//Age char id[15];//School Number }s1, s2, s3; void print1(struct stu sp1) { printf("%s\n", sp1.name); } void print2(struct stu* sp2) { printf("%s\n", sp2->name); } int main() { struct stu s4 = { "Zhang San", 15, "20220805" }; print1(s4); print2(&s4); return 0; }
So which one is better for two functions, print1 and print2?
The answer is the print2 function
Parameters need to be stacked when passing function arguments.
If a structure object is passed, the structure is too large and the parameter stack has a high overhead, resulting in performance
Falling.
Conclusion: When the structure is transmitted, the address of the structure should be transmitted.
summary
Structures are a relatively simple part, but they are also an important part of the process of learning data structures. People should be familiar with the structure. (Limited level, welcome big guys to correct if there are any mistakes! Thank you!!!)