C language custom type combing

C language custom type combing

​ I haven't updated my blog for a long time, and I'm a little rusty. During this period of time, although the blog has been stopped, I still continue to learn. Today, I want to introduce custom types in C language to you.
insert image description here

structure

1. What is a structure?

	在生活中,很多的复杂对象无法用C语言的某一种数据类型来描述,比如一个学生它有自己的名字,性别,年龄。这些信息我们无法用某一种基本数据类型来描述,那么我们可以用结构体将这些信息整合到一起。比如这样:
struct student
{
    
    
    char name[20];//名字
    int age;//年龄
    char sex[5];//性别
};//注意千万不能少分号

​ So we have successfully declared a struct.

2. How to access the structure members?

There are generally two access methods as follows:

​ 1. We can use the structure access symbol (.), the member access operator is a period between the structure variable name and the structure member we want to access. Let's take a look at the example below.

#include <stdio.h>
#include <string.h>

struct student
{
    
    
    char name[20];
    int age;
    char sex[5];
};

int main()
{
    
    
 	struct student s1;
    strcpy(s1.name, "张三");
    s1.age = 18;
    strcpy(s1.sex, "男");
    printf("%s\t%d\t%s\n", s1.name, s1.age, s1.sex);
    return 0;
}

The results are as follows:
insert image description here
​ 2. We can also use the structure pointer to the structure to access the structure members. An example is as follows:

#include <stdio.h>
#include <string.h>

struct student
{
    
    
    char name[20];
    int age;
    char sex[5];
};

int main()
{
    
    
 	struct student s1;
    struct student* p = &s1;
    strcpy(p->name, "张三");
    p->age = 18;
    strcpy(p->sex, "男");
    printf("%s\t%d\t%s\n", s1.name, s1.age, s1.sex);
    return 0;
}

3. How to calculate the size of the structure

First let's look at this structure

struct s1
{
    
    
    char c1;
    int i;
    char c2;
};
printf("%d",sizeof(struct s1));

What do you think is the size of this structure? If you simply add the size of the type, then the size of this structure should be 6, so let's try it, what is the result?

insert image description here
insert image description here

​Why 12? ? ? ? ? ?

​ I searched left and right on the Internet. Finally found the truth of the matter. It turns out that there is a structure alignment operation for the structure, so what is the structure alignment?

In short, when the members of the structure are stored in memory, they are not simply put together in order, but follow a certain rule. Next, let me unravel the mystery of this wonderful phenomenon.

结构体对齐的规则:
    1,第一个成员在与结构体变量偏移量为0的地址处。
    2,其他成员变量要对齐到对齐数的整数倍的地址处。
    3,结构体总大小为最大对齐数(每个成员都有自己的对齐数)的整数倍
    4,如果嵌套了结构体的情况,嵌套的结构体对齐到自己的最大对齐数的整数倍,结构体		   的大小就是所有最大对齐数的整数倍

First let me explain a few terms:

​ Offset: refers to the difference between the address of the structure member and the first member of the structure.

​ Alignment: the smaller value of the compiler's default alignment number and the size of the member.

Let's see how the above structure is calculated

struct s1
{
    
                 成员大小  默认对齐数(这里我用的vs默认对齐数为8)   对齐数
    char c1;   1          8                                 1
    int i;     4          8                                 4
    char c2;   1          8                                 4
};

Then the placement of this structure in memory is shown in the following figure:

insert image description here

According to the third rule, the size of the structure is an integer multiple of the maximum alignment number, so the size of this structure is a multiple of 4 so it is 12.

今天的知识分享就到这里了,我们下次再见,拜拜

insert image description here

Guess you like

Origin blog.csdn.net/m0_60447315/article/details/123149783