Structure and other data forms

1. Structure

    Guidance——In practical problems, sometimes we need several different types of variables to modify a variable. For example, a student’s information requires student ID (integer), name (string), and grade (floating point) ) Wait, these data types are all different but they represent a whole. If there is a connection, then we need a new data type. So we introduced the structure.

1. The declaration of the structure

//声明一个结构体
struct student
{
    
    
    char name[20];//一个字符串表示学生姓名 ; 
    char num;//一个整型表示学生学号 ; 
    float score;//一个浮点型表示学生成绩; 
};//注意分号不能少,这也相当于一条语句;

    This declaration describes a structure composed of 1 character array, 1 int integer and 1 float variable, but note that he did not create an actual data object, but describes an element that composes this type of object , [Therefore, we sometimes call the structure declaration a template, because it outlines how the data should be stored, and does not instantiate the data object].
Let's introduce the above structure declaration;

  1. First use the keyword struct, which means that the next is a structure.
  2. An optional mark (book) follows, which is a quick mark used to refer to the structure. So we will be able to create data objects like this in the future
struct student stu;
//把stu设为一个可以使用student结构体的结构体变量,则stu这个变量就包含了其student结构体中的所有元素
  1. Next is a curly brace that encloses the list of structure members, and each member variable is described by its own declaration method, and the description is ended with a semicolon;
    note; each member can use any A c data structure or even other structures are also possible;
  2. The semicolon after the closing brace indicates the end of the structure design definition (not less).
  3. Regarding the location of its struct declaration, that is, where to put this code. This is also scoped.
    If this kind of declaration is placed outside any function, then the optional mark can be used in this file, and all functions following the declaration can be used. If this kind of declaration is inside a function, its mark can only be used internally, and after its declaration.
  4. The definition of our structure type (the declaration of the structure) just told the compiler how to represent the data, but it did not let the computer allocate space for it. If we want to use a structure, we need to create a variable, that is, a structure variable;
    create a structure variable; struct stduent stu;
    see this instruction, the compiler will create a structure variable stu, then the compiler The memory space will be allocated for the variable according to the studnt template, and the storage space here is combined with this variable. This is also when accessing the structure variable members later, we will use the structure variable name to access.

The role of struct book In the structure declaration, struct student plays the same role as int and other basic data type names.

2. Initialization of the structure

struct student stu={
    
      //对结构体初始化 
        "xiaoming",//姓名name为字符串 
        1001,     //学号num是整型
        120.5    //成绩score为flaot型 
    };
    //要对应起来,用逗号分隔开来,与数组初始化一样;

It is worth noting that:

  • Regarding the problem of structure initialization and storage class period:
    If you want to initialize a structure with static storage period, the value in the initialization item list must be a constant expression. If the storage period is automatic, then the value of the list does not need to be a constant Up.

  • If the structure variables are not initialized when they are defined, they cannot be initialized all together later.
    it means:

——————这样是可以的,在定义变量的时候就初始化了。
struct student stu={
    
      //对结构体初始化 
        "xiaoming",//姓名name为字符串 
        1001,     //学号num是整型
        120.5    //成绩score为flaot型 
    };
——————这种就不行了,在定义变量之后,若再要对变量的成员赋值,那么只能单个赋值了。
struct student stu;
    stu={
    
     
       	  "xiaoming",
      		1001, 
       		120.5 
    };//这样就是不行的,只能在定义的时候初始化才能全部赋值,之后就不能再全体赋值了,只能单个赋值。
——————只能
    stu.name = "xiaoming";   //单个赋值;

3. Access structure members

    The structure is like a super array. In this super array, one element can be of char type, the next element can be of flaot type, and the next element can be of int array type. These all exist. In the array, we can access each element of an array through the subscript, so how to access each member in the structure?

Use the structural member operator dot (.).
    Structure variable name. Member name;
note that its associativity is from left to right, and it has the highest priority among all operators.
    For example, s1.title refers to the title part of s1, and stu.name refers to Is the name part of stu. Then you can use s1.name like a character array and stu.score like a float data type.
    Note that although stu is a structure, stu.score is of type float, so stu.score is equivalent to the variable name of the float type and is used according to the float type.
Such as: printf("%s%s\n%f",s1.title,s1.author,s1.value);//Access structure variable elements

    Note: s canf("%d",&s1.value); There are two operators in this statement, & and the structural member operator point. According to the truth, we should enclose (s1.value, because they are a whole, which means s1 The value part) but we do not enclose it is the same, because the priority of the point is higher than the &.
    If the member itself is a structure type, then several member operators can be used to find the lowest level member level by level and then operate on it.
Structure variable name. Members. Sub-members...... The lowest-level sub-members.

Whole and separate:

  • You can assign a structure variable as a whole to another structure variable of the same type to achieve the effect of overall assignment; the value of this member variable will all be assigned to another variable as a whole.
  • It is not possible to input and output a structure variable as a whole; when inputting and outputting structure data, each member of the structure variable must be specified separately.

4. Structure array

    Why refer to the structure array? Obviously, in the book type structure above, each book needs to be described by a book type structure variable. If you want to describe two books, you need to use two such variables, in turn Analogy; therefore, an array of the structure is used to represent these books; and the array is to store a set of data of the same type, so there is the appearance of the structure array, pay attention to the original setting,

1. Declare structure array

As with ordinary array declarations, int a[10]; int is the data type of the element, and a is the array name [10], indicating that the memory of the int unit of 10. Look at the structure declaration; struct student stu[10]; is it similar, struct student is the data type of the array element, stu is the array name, and [10] is the memory of 10 struct student units.
Explanation: Declare that library is a specific array of 10 elements, and each element has a structure of student type, so you can get stu[0], stu[1]...... All are a separate book structure.
Note that the library itself is not a structure name but an array name.

2. Access the members of the structure array

Rule: Add a dot operator after the structure name, and then the member name.
stu[5].name; //Indicates the name member of the fifth element, stu[5] is the name of the structure variable, and name is the member name;
stu[5].name[4];//Note that name is an array type , The fourth character of the name member of the fifth array element; access to the members of the structure array.
The benefits of using a pointer to a structure; just like a pointer to an array, it is easier to manipulate than the array itself, and a pointer to a structure is usually easier to manipulate than the structure itself;

3. Declare and initialize the structure pointer

Declare struct student * him; the
rule is, struct structure name + * + pointer name;
this declaration does not create a new structure, but creates a pointer variable him pointer, which can point to any existing student type The structure;
him = &stu[0]; the
pointer him is pointing to the structure stu[0], how to use him to obtain a member of stu[0]?
Method 1:
Introduce an operator:
The structure pointer followed by the -> operator is the same as the structure name followed by the dot operator.
One thing to note is that you cannot use the him. member, because him is not a structure name.
Method 2:
If him=&stu[0], then him=stu[0]; because & and is a reciprocal operator;
& takes address, * takes value;
=>stu[0].score is equivalent to ( *him).score; note that parentheses must be used, priority issues;
then all have the same effect as him.score.

Guess you like

Origin blog.csdn.net/qq_52355487/article/details/111460919