[introduction to C / C + + programming] C language structure hard core play sharing, everything is data!



There are too many applications for structures. In today’s article, I mainly summarize some unique hard core tips about structures. For more excellent programming performance of structures, if you don’t have the basic knowledge of structures, you have to go back to the column tutorial or find a book to learn by yourself.


Structure: literally, it means a data form with a hierarchical structure. The so-called hierarchical structure is the various members defined in the structure. In vernacular, a structure is a data packet that can contain all kinds of data. For this sentence, we need to dig deeply into “all kinds of data”. For our programming – “everything is data”. Further understanding, the structure can include what you want to include in the whole program. If this kind of deliberation is correct, the structure really has something. Draw a picture for everyone to guess. The wonders of the structure are all in the picture!



Initialization of hard core tips

Most of our novice partners usually initialize the structure variables directly. (haha, many partners may not initialize the variables at all.) however, we still need to develop better programming habits. Although most integrated development environments will initialize some global variables to 0 for you, but for the portability of the code In consideration of predictability, it is suggested that since it is defined, an initial state should be given.


Analyze: the above is a very simple code. The initialization of the first structure is a very common way in the Linux source code. The compiler must follow the ISO C99 standard, otherwise it can only use the second conventional way. However, most compilers support the standard, so it is also widely used, The benefits are as follows: 1) the initialization of structure member variables is clearer, especially when the structure is particularly large; 2) Don’t care about the order of initialization, just the correspondence between name and data, which is very flexible.

Multiple aliases of hard core tips

When you program in C for a long time, you will use structures to encapsulate variables with the same attributes, so the same structure may be used in multiple places, and the functions of the structure will be different, just like a person who may be a student or a waiter outside, If this person doesn’t wear clothes or make a mark, it may sometimes be difficult for us to distinguish, so how will we change their clothes in the C program? The simple code is as follows:


Analysis: the above code is really simple. Some partners may be able to understand it at a glance, but this method is very valuable, especially when we play the algorithm in the future. For example, the nodes are the same, but the nodes are divided into child nodes and parent nodes. Although the parameters passed in by some processing functions are the same, they have different meanings. We can deal with them in this way, This increases the readability of the code.

Hard core tips about 0 address

The content of this section is the most hard core content of this article. You guys should take notes. One way to deeply learn C language is to read more big guys’ code and be good at summarizing some commonly used tips for you to use. Today, take two macro definitions in Linux kernel to share some structure tips:

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

#define container_of(ptr,type, member) ({ \

consttypeof( ((type)0)->member ) __mptr = (ptr); \

(type)( (char )__mptr – offsetof(type,member) );})


The function of the first macro definition is to obtain the offset of a structure member from the first address of the structure. Parameter type: structure type; Parameter member: structure member. In fact, this is relatively simple. Convert the 0 address mandatory type into a structure type pointer, and then point to the member through the structure to obtain the structure member variable, and then get the structure member address through & to obtain the structure member address. The offset of the member = (structure member address – structure head address); However, the first address of the structure is 0, so the offset of the member = the address of the structure member should be clear enough.

The function of the second macro definition is to obtain the corresponding structure first address (that is, the structure address) through the structure member variable

Parameter PTR: structure member variable address;

Parameter type: structure type;

Parameter member: structure member.

This macro definition may be a little difficult to write for some small partners, but it is mainly divided into two parts,

In the first part, the type of member is obtained through typeof and a const pointer is defined. The purpose of const is to prevent the user from writing the content of 0 address. Reading and writing illegal areas on most chips will cause exceptions.

In the second part, the offset of the structure member relative to the first address of the structure is obtained by using the offset of macro definition. In this way, the address of the structure to which the current structure member belongs is obtained. The principle formula is: (structure address = structure member address – structure member offset).

Here’s a general note: in the second point, we talked about the typeof keyword, which is an extended keyword in the gun C standard, so you need to pay attention when using the macro definition, otherwise you may report an error when compiling with other standards.

Hope to help you!

Self taught C / C + + programming is very difficult. If you want to improve your programming ability and the level of writing projects faster, welcome to grow together!

Have you learned a lot of new knowledge here~

If you really want to learn programming, Xiaobian recommends meC language / C + + programming learning base [Click to enter]!

It’s all about learning programming, guys. It’s still simple to take you in. Learn together and cheer together~

There are many learning materials and videos, I believe you will like it!

Involving: game development, common software development, basic programming knowledge, curriculum design, hackers, etc