The essence of the structure is a data abstraction in C language. In layman's terms, it is a reorganization of basic data types. Why is restructuring required? Because the basic data type is not enough. Why is it not enough? Because too many types of information are required. This is a big topic. There are no different types of information, but in order to facilitate internal computer management, people divide information into several basic types in C language, for example, integer, floating point, complex, and Boolean. However, it is sometimes not enough to describe all the information of a thing with only one basic type. For example, the basic attributes of a book are author and price), publication date (I don't know what type), title (simplified type ). However, we are not dealing with a specific attribute of this book, but the whole, at this time, we had to combine various types to construct a brand new data type-this is the time to make full use of our imagination and creativity. In addition to the basic types, the new types combined by them are collectively referred to as struct.
1. In C, to create a new data type, you must first define this type. Tells the Compiler which basic types the new type is composed. The standard format is as follows:
Struct book
{
Int price;
Char title [44];
};
The first is the keyword struct, indicating that this is not a basic type; then it is a tag, which is equivalent to a name for the new type, but this name is not required, next we will talk about why it is better to write it. The new type of member should be put in curly brackets to indicate the basic data type. Note that a semicolon must be added at the end of a declaration because it is only a type definition and must end with a semicolon. The type definition does not allocate memory space. Instead, it creates a template to allocate memory only when variables are defined using this template.
2. Define struct Variables
Variables can be defined with the type. For example:
Struct book
{
Int price;
Char title [44];
} A, B;
A and B are defined as struct book variables, and memory space is allocated according to the template.
You can also define variables as follows:
Struct
{
Int price;
Char title [44];
};
The difference between the two forms is that one has a tag and the other does not. However, no matter which one is a little bloated, especially when there are many Members. At this time, the role of the tag is displayed. We can simplify the definition form:
Struct book a, B;
Note that struct cannot be omitted. tags must be used with struct to be equivalent to basic types such as int and float. Without tags, variables cannot be defined directly. In addition, the role of a tag is not limited to simplifying the definition form. In fact, this template (New Type) can be used repeatedly (that is, defining variables) without confusion. As shown below:
Struct
{
Int price;
Char title [44];
};
Struct
{
Int price;
Char title [44];
} * B;
Note that the two structs are different, at least in the eyes of the compiler, even if their members are the same. For example, B = & a; is invalid. With tags, you can avoid such confusion. tags can set the same structure of members to the same type by default.
In addition, variables can only be defined without tags. In other words, the following type definition (Declaration) is meaningless:
Struct
{
Int price;
Char title [44];
};
3. Initialization
You can do this:
Struct book a = {43.5, "agv "};
The members are separated by commas (,), and the member sequence must be consistent with the template, that is, the type must match. If the number of members is greater than the value declared by the template, an error is returned. If the number of members is less than the number of templates, the unspecified value of the project is set to 0 or null. If the number of members is smaller than the number of templates, no error is returned.
You can also do this:
Struct book a = {. title = "agv",. price = 43.5 };
In this form, you do not need to assign values in the order of the template, but note that if:
Struct book = {. title = "agv",. price = 23.1, "ree "};
So what is the final title value? The answer is "ree", which will be replaced later. Because the title is indeed after the price, after the price value is assigned, it is natural to continue to assign the title value.
During initialization, if the struct variable is a global variable, you must use a constant expression to initialize the member. If the struct variable is a local variable, it can be a variable expression to initialize the member.
4. Assignment
Assignment and initialization are different (how painful to comprehend )!!!!!
Struct basically does not assign values. For example, the following method is wrong:
Struct book;
A = {. title = "agv",. price = 43.5 };
The same is true for arrays. However, struct variables can be assigned values to each other, for example:
Struct book a, B;
A = B;
Of course, it only stops at local variables.
5. Access member variables
There are three methods to combine two symbols:. And->.
Struct book;
Printf ("a. title \ n ");
Struct book * B;
B = &;
Printf ("B-> title \ n ");
Struct book * B;
B = &;
Printf ("(* B). title \ n ");
Here, we can see B-> title = a. title, but-> is used for pointers, and. is used for structure variables.
There are so many basic content that will be added later.