Home  >  Article  >  Backend Development  >  Detailed explanation of struct usage in C language

Detailed explanation of struct usage in C language

藏色散人
藏色散人Original
2020-03-03 13:48:528207browse

Detailed explanation of struct usage in C language

Detailed explanation of the usage of struct in c language

In C language, you can use a structure (Struct) to store a Group different types of data. The definition form of a structure is:

struct 结构体名{
    结构体所包含的变量或数组
};

A structure is a collection that contains multiple variables or arrays. Their types can be the same or different. Each such variable or array is called Is a member of the structure.

Recommended "c Language Tutorial"

Please take a look at the following example:

struct stu{
    char *name;  //姓名
    int num;  //学号
    int age;  //年龄
    char group;  //所在学习小组
    float score;  //成绩
};

stu is the name of the structure, which contains 5 members , respectively name, num, age, group, score. Structure members are defined in the same way as variables and arrays, except that they cannot be initialized.

Pay attention to the semicolon after the curly braces; it cannot be missing. This is a complete statement.

The structure is also a data type, which is defined by the programmer himself and can contain multiple other types of data.

Data types such as int, float, char, etc. are provided by the C language itself and cannot be split. We call them basic data types; and structures can contain multiple basic types of data, as well. Can contain other structures, which we call complex data types or constructed data types.

Structure variables

Since the structure is a data type, you can use it to define variables. For example:

struct stu stu1, stu2;

defines two variables stu1 and stu2, both of which are stu type and consist of 5 members. Note that the keyword struct cannot be missing.

stu is like a "template", and the variables defined have the same properties. The structure can also be compared to a "drawing" and the structure variables to "parts". Parts produced based on the same drawing have the same characteristics.

You can also define structure variables while defining the structure:

struct stu{

char *name; //Name

int num ; //Student number

int age; //Age

char group; //Study group

float score; //Score

} stu1, stu2;

Just put the variable at the end of the structure definition.

If you only need two variables, stu1 and stu2, and you don’t need to use the structure name to define other variables later, you don’t need to give the structure name when defining, as shown below:

struct{ //No stu

char *name; //Name

int num; //Student number

int age; //Age

char group; //Study group

float score; //Score

} stu1, stu2;

This is easy to write, but because there is no structure name , you cannot use this structure to define new variables later.

Theoretically speaking, each member of the structure is stored continuously in the memory, which is very similar to an array. For example, the memory distribution of the above structure variables stu1 and stu2 is as shown in the figure below, occupying a total of 4 4 4 1 4 = 17 bytes.

However, in the specific implementation of the compiler, there may be gaps between each member. For stu1 and stu2, there is a 3-byte blank filling between the member variables group and score (see the figure below) . Calculated in this way, stu1 and stu2 actually occupy 17 3 = 20 bytes.

Regarding the reasons for the "cracks" between member variables, we will explain in detail in the section "C Language Memory Alignment to Improve Addressing Efficiency" in the "C Language Memory Essentials" topic.

Acquisition and assignment of members

Structures are similar to arrays. They are also a collection of data, and their overall use does not make much sense. Arrays use subscript [ ] to obtain a single element, and structures use dot. to obtain a single member. The general format for obtaining structure members is:

结构体变量名.成员名;

In this way, you can obtain the value of the member, and you can also assign a value to the member:

#include <stdio.h>
int main(){
    struct{
        char *name;  //姓名
        int num;  //学号
        int age;  //年龄
        char group;  //所在小组
        float score;  //成绩
    } stu1;
    //给结构体成员赋值
    stu1.name = "Tom";
    stu1.num = 12;
    stu1.age = 18;
    stu1.group = &#39;A&#39;;
    stu1.score = 136.5;
    //读取结构体成员的值
    printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!\n", stu1.name, stu1.num, stu1.age, stu1.group, stu1.score);
    return 0;
}

Running result:

Tom's The student number is 12, the age is 18, and in Group A, this year’s score is 136.5!

In addition to assigning values ​​to members one by one, you can also assign values ​​as a whole during definition, for example:

struct{
    char *name;  //姓名
    int num;  //学号
    int age;  //年龄
    char group;  //所在小组
    float score;  //成绩
} stu1, stu2 = { "Tom", 12, 18, &#39;A&#39;, 136.5 };

However, overall assignment is limited to when defining structure variables. During use, you can only assign values ​​to Members are assigned values ​​one by one, which is very similar to the assignment of arrays.

It should be noted that the structure is a custom data type, which is a template for creating variables and does not occupy memory space; only structure variables contain real data and require memory space to store .

For more programming related content, please pay attention to the Programming Introduction column on the php Chinese website!

The above is the detailed content of Detailed explanation of struct usage in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn