[C language] structure type and structure variable (byte aligned)

table of Contents

One, structure type definition

Second, the definition of structure variables

① Define the structure type first, and then define the structure variable

② While defining the structure type, define the structure variable

③Define structure variables directly

Three, the memory allocation of structure variables

Fourth, the initialization of structure variables

Five, the reference of structure variables


One, structure type definition

A structure is a structured data type that can integrate different types of data. Each piece of data is called a member of the structure type. When using a structure, you first need to define the structure type. The definition of the structure type is as follows:

struct structure type name

{

    Data type member name 1;

    Data type member name 2;

  。。。

};

//"struct" is a keyword that defines the type of structure

Example demonstration:

Student information includes: student number (num), name (name), gender (sex), age (age), address (address), which is defined as a structure named Student.

struct Student
{
    int num[10];
    char name[10];
    char sex;
    int age;
    char address[30];
};

[Note] A member in a structure type can also be a structure variable.

Case Analysis:

struct Date
{
    int year;
    int month;
    int day;
};

struct Student
{
    int num[10];
    char name[10];
    char sex;
    struct Date birthday;//出生日期birthday为结构体类型Date
}stu1;

At this time, the type structure of the structure type Student is as follows:

【Notes】

  • The structure type definition starts with "struct", followed by the name of the structure type, and the naming rule of the name is consistent with that of the variable
  • After defining a structure type, it does not mean that a memory unit is allocated to store each data member immediately . It just tells the compiler which data type members the structure type is composed of, how many bytes each occupies, and what format Store them and treat them as a whole.
  • The semicolon in the bracket at the end of the structure type definition is essential .
  • A member of a structure type can be a variable, but cannot be a variable of its own structure type.

Second, the definition of structure variables

The structure type is defined, and the system will not allocate memory space for it. In order to be able to use structure type data in the program, you should define structure type variables and store specific data in them. There are three main methods for defining structure variables:

① Define the structure type first, and then define the structure variable

struct Student
{
    int num[10];
    char name[10];
    char sex;
    int age;
    char address[30];
};

struct Student stu1,stu2;
//stu1、stu2为结构体类型变量,各自可以存储一组基本类型的变量,且分别占据一块连续的内存空间

② While defining the structure type, define the structure variable

struct Student
{
    int num[10];
    char name[10];
    char sex;
    int age;
    char address[30];
}stu1,stu2;
//变量stu1和stu2的数据类型都是结构体类型Student

③Define structure variables directly

struct 
{
    int num[10];
    char name[10];
    char sex;
    int age;
    char address[30];
}stu1,stu2;
//stu1、stu2为结构体变量,但定义的结构体没有类型名称(又称“匿名结构体”)

[Note] The structure type is a user-defined data structure. It is the same as the simple data type . No space is allocated for the structure type at compile time. Only when it is used to define a variable, a memory unit of the size required by the structure type will be allocated for the structure variable.


Three, the memory allocation of structure variables

Once the structure variable is defined, the system will allocate memory space for it. The memory size occupied by structure variables is allocated according to the byte alignment mechanism.

Byte alignment: Bytes are arranged in space according to certain rules. Byte alignment follows two rules:

The offset of each member variable of the structure relative to the first address of the structure is an integer multiple of the size of the basic data type (excluding structure, array, etc.) of the member variable; Add padding bytes in between.

Case Analysis:

struct
{
    char a;
    double b;
    int c;
    short d;
}S;

The distribution of each member in the structure variable S in the memory is as follows:

Why are there 7 bytes of padding?

The address of the member variable a is the first address of the structure variable S, which occupies 1 byte; the member variable b occupies 8 bytes, but if the byte follows the variable a, it violates the byte alignment principle①, so at least 7 bytes are filled in front so that the address of the variable is an integer multiple of the first address. The offsets of the variables c and d relative to the first address are 16, and 20 bytes are exactly multiples of 4 and 2.

The total size of the structure is an integer multiple of the size of the longest structure member variable. If it is not enough, the compiler will add padding bytes after the last member

According to the principle ①, the memory size occupied by the above-mentioned structure variable S is calculated to be 22 bytes, but this does not conform to the principle of byte alignment ②, not a multiple of 8, so the compiler will fill two bytes at the end to make It becomes 24 bytes.

#include <stdio.h>

struct
{
    char a;
    double b;
    int c;
    short d;
}S;

int main() {
    printf("结构体变量S占字节大小为:%d\n", sizeof(S));
}

operation result:


Fourth, the initialization of structure variables

The structure variable stores a group of different types of data. Therefore, the process of initializing the structure variable is actually the process of initializing the members of the structure.

There are two ways to initialize structure variables:

① While defining the structure type and structure variable, initialize the structure variable

struct Student
{
    int num;
    char name[10];
    char sex;
    int age;
}stu={1001,"Li Lei",'M',18};
//结构体变量stu定义的同时进行了初始化

② Define the structure type first, and then initialize the structure variable while defining it

struct Student
{
    int num;
    char name[10];
    char sex;
    int age;

};

struct Student stu={1001,"Li Lei",'M',18};

[Note] When initializing the structure, if only some of the members are initialized, only the first members need to be initialized, and the latter members can be vacant, because when assigning member variables, the compiler matches the members from front to back instead of Automatic matching by data type


Five, the reference of structure variables

The purpose of defining and initializing structure variables is to use the members of structure variables. The way of quoting structure variables is as follows:

Structure variable name . Member name

#include <stdio.h>

struct Student
{
    int num;
    char name[10];
    char sex;
    int age;
  
};

struct Student stu = { 1001,"Li Lei",'M',18 };

int main() {
    printf("学生信息:\n");
    printf("学号:%d\n", stu.num);
    printf("姓名:%s\n", stu.name);
    printf("性别:%c\n", stu.sex);
    printf("年龄:%d\n", stu.age);
}

operation result:

 

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/109168583