Getting to know the structure

1. Composite types and struct types

Types that are composed of basic types according to grammar rules are called composite types. The structure is composed of struct, composite type, custom identifier (called Tag in C language) and ; . For example: struct complex_struct{int x,y}; Note that whether the variable is defined as shown in Figure 1-2 , or only the Tag is defined as shown in Figure 1-1 , the declaration must end with " ; " .

There are three ways to write structures:

1. When defining the structure type, the variables are also defined as shown in Figure 1-2. Tag can sometimes be omitted in this case. For example:

struct{

                                double x,y;

}z1,z2;

But then there is no way to refer to the struct type because it doesn't have a name. Each structure is composed of several members, we can use the variable name plus operator "." to access. For example the following picture:


Figure 1-2 Defining and accessing structures


2. The structure is defined in the global scope, and the Tag defined in this way can be used in each function after its definition. Figure 1-1;

Figure 1-1 Defining and accessing structures

Struct variables can also be initialized at definition time, for example:

struct stu boy1={20,"丽萨","女"};

The initialized data is assigned to each member of the structure in turn. The compiler will report an error if the initialization has more data than the structure has members, but it's not an error if it's just a trailing comma. If there is less data in initialization than the structure has members, unspecified members will be initialized with 0. The following forms of initialization are legal:

int age=14;

struct stu boy1={age,"Lisa","Female"}; /*boy1.age=14,boy1.name=Lisa, boy1.sex=female*/

struct stu boy1={"Lisa","Female"}; /*boy1.age=0,boy1.name=Lisa, boy1.sex=female*/

Note that boy1 must be a local variable to initialize its members with the value of another variable age. If it is a global variable, it can only be initialized with a constant expression. Whether initializing global variables or local variables {} This syntax cannot be used for struct copying. E.g:

struct stu boy1;

boy1={20,"Lisa","Female"};

Struct types have many restrictions in expressions and are not as free as primitive types. For example +, -, *, / and other arithmetic operators and &&, ||, ! and other logical operators cannot be used for structure types, and the value of control expressions in if statements and while statements cannot be of structure types. Using the assignment operator between struct variables is allowed. It is allowed to initialize one struct variable with another struct variable, for example:

struct   arry_struct  {double x,y};                    --------1

struct   arry_struct    z1={3.0,4.0};                  --------2

struct   arry_struct    z2=z1;                          --------3

z1=z2; --------4

Also z2 must be a local variable to be initialized with the value of variable z1. Since structure variables can be initialized by mutual assignment, they can also be passed as parameters and return values ​​of functions: (The Tag arry_struct defined in the above example can be directly replaced by strct arry_struct instead of the type name. For example, in the second line, The third row).


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325511827&siteId=291194637