How to master the structure of core knowledge side by side with pointer in C language?

Time:2021-12-31

C language is a legendary language in the programming world. After decades, it still ranks in the forefront.

This paper mainly talks about the structure in C language. Structure is not only an important part of C language, but also a common data structure in C language.

1、 About structures

In C language, struct refers to a data structure, which is a kind of aggregate data type in C language.

Structures can be declared as variables, pointers or arrays to implement more complex data structures. A structure is also a collection of elements. These elements are called members of the structure, and these members can be of different types. Members are generally accessed by name.

The definition of structure is as follows:

struct tag { member-list } variable-list;

Of which:

✿ struct is the structure keyword;

✿ tag is the sign of the structure;

✿ member list is a structure member list, which must list all its members;

✿ variable list the variables declared for this structure.

In general, at least two of these three parts: tag, member list and variable list.

 

2、 General definition of structure

Different definitions are applied in different scenarios. Therefore, we need to define the structure in combination with the actual situation when programming.

Method 1:

Define the structure stu. At this time, the structure is equivalent to a type, such as int. if you need to use this structure, the method is the same as int

struct stu

{

    char  aa;

    short bb;

    int  cc;

};

struct stu stu1, stu2;

Method 2:

Define the structure stu and define the structure variables stu1 and stu2 to be used. If you need to define structure variables later, the method is the same as 1

struct stu

{

    char  aa;

    short bb;

    int  cc;

}stu1, stu2;

struct stu stu3;

Method 3:

When defining a structure, the structure name defaults, and the structure variables stu1 and stu2 are defined at the same time. However, structure variables cannot be defined later.

struct

{

    char  aa;

    short bb;

    int  cc;

}stu1, stu2;

Tips:

Structure variables cannot be defined here as above: (the following error)

struct stu3;

struct stu stu3;

 

3、 Using typedef to define a structure

Let’s briefly talk about typedef

Typedef is a keyword in C and C + + programming languages. It is used to alias a data type in order to make the source code easier to read and understand. It is often used to simplify the structure of declaring complex types, but it is also often seen in integer data types of various lengths, such as size_ T and time_ t。

 

Method 4:

Use typedef to define the structure. At the same time, give the stu structure the alias stu. For subsequent definitions, you can use stu directly without using struct stu.

typedef struct stu

{

    char  aa;

    short bb;

    int  cc;

}STU;

STU stu1;

You will find that stu replaces struct stu through the keyword typedef.

Of course, it can also be used as follows:

struct stu stu1;

The above definition loses the meaning of typedef, so it is not recommended.

Method 5:

When using typedef to define a structure, omit the first alias stu of the structure and add stu directly after it. The use method is the same as above.

typedef struct

{

    char  aa;

    short bb;

    int  cc;

}STU;

STU stu1;

Method 6:

There is also a definition that conforms to grammatical rules but has little meaning.

typedef struct stu

{

    char  aa;

    short bb;

    int  cc;

};

struct stu stu1;

These definitions are grammar knowledge. If you don’t understand them, please review them again.

 

4、 Structure size calculation

I believe many people don’t understand the size of the structure. In practical programming, it is also applied in many places. For example, when storing and copying structures, the size of structures will be involved.

1. Compare the size of the two structures. Are they the same?

Structure 1:

struct stu

{

    char  aa;

    short bb;

    char  cc;

}stu1;

Structure 2:

struct stu

{

    char  aa;

    char  bb;

    short cc;

}stu2;

The answer is different. The program test shows that sizeof (stu1) = 6 and sizeof (stu2) = 4

2. Analyze different reasons

The structure calculation shall follow the byte alignment principle and generally meet three criteria:

1) The first address of a structure variable can be divided by the size of its widest basic type member;

2) The offset of each member of the structure relative to the first address of the structure is an integer multiple of the size of the member. If necessary, the compiler will add padding bytes between the members;

3) The total size of the structure is an integer multiple of the size of the widest basic type member of the structure. If necessary, the compiler will add padding bytes after the last member;

 

For stu1, the maximum byte is 2 bytes, and the order is char short char:

2 bytes

 

For stu2, the maximum byte is 2 bytes, and the order is char – > char – > short:

 

Through the above two tables, I believe you should understand that it is the same principle to change it to a 4-byte int.

Finally, I would like to emphasize that learning programming should not only read books, but also type more code.

 

If you want to study deeplyC language and advanced programming – Programming Club [Click to enter]!

It involves: C language, C + +, windows programming, network programming, QT interface development, Linux programming, game programming, hackers and so on

 

Introduction to programmer programming:

 

2. Recommended books for programmers:

 

An active, high-level programmer programming learning hall; The introduction to programming is only incidental, and the improvement of thinking is valuable!