Home > Backend Development > C#.Net Tutorial > What about C language structure type variables during program running?

What about C language structure type variables during program running?

青灯夜游
Release: 2022-12-30 11:13:09
Original
10471 people have browsed it

C language structure type variables all members remain in memory during the running of the program. Reason: Variables of structure type occupy continuous memory units as a whole during program running.

What about C language structure type variables during program running?

The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.

C language structure type variables all members remain in memory during the running of the program.

C Language Structure (Struct)

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

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

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. Please look at the following example:

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

stu is the structure name, which contains 5 members, namely name, num, age, group, and 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.

Structure is also a data type, which is defined by the programmer himself and can contain multiple other types of data. (For basic knowledge related to C language, you can refer to: "C Language Tutorial Video")

Data types such as int, float, char, etc. are provided by the C language itself and cannot be used anymore. When split, we call it a basic data type; and a structure can contain multiple basic types of data or other structures, which we call a complex data type or a constructed data type.

Structure variables

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

struct stu stu1, stu2;
Copy after login

defines two variables stu1 and stu2, both of which are of type stu 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. Variables of structure type occupy continuous memory units as a whole during program running.

You can also define structure variables while defining the structure:

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

Place the variables at the end of the structure definition.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of What about C language structure type variables during program running?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template