Follow+Star Public Account Number, don’t miss out on exciting content

Author | strongerHuang
WeChat Public Account | Embedded ColumnC language has been around for over 50 years and is considered a legendary programming language, still leading the way today.This article will discuss the structure in C language, which is a key reason for the enduring popularity of C.Why is that? Becausestructures are a commonly used data structure in C language and an important part of it.C language maintains the characteristics of efficient and low-code, and through structures, it continues the object-oriented features.It can be said thatwithout structures, C language would lack its soul..Here, we will focus on some common uses of C structures in embedded projects.
About Structures
In C language, a structure (struct) refers to a data structure, which is a type of aggregate data type.
Structures can be declared as variables, pointers, or arrays to implement more complex data structures. A structure is also a collection of elements, which are called members of the structure, and these members can be of different types.Structure definition is as follows:
struct tag { member-list } variable-list;
Where:
- struct is the keyword for structure;
- tag is the identifier for the structure;
- member-list is the list of members of the structure, which must list all its members;
- variable-list are the variables declared for this structure.
In general, at least two of the three parts: tag, member-list, and variable-list must appear.
Common Structure Definitions
Different definitions apply in different scenarios, so we need to define structures based on actual situations when programming.
Method 1: Define structure stu, at this point the structure is equivalent to a type, like int, and 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 structure stu while also defining the structure variables stu1, stu2. 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: Define the structure without a name, while defining structure variables stu1, stu2. However, you cannot define structure variables again later.
struct { char aa; short bb; int cc; } stu1, stu2;
Note: You cannot define structure variables like above again: (the following is an error)
struct stu3; struct stu stu3;
Using typedef to Define Structures
Here, let’s briefly talk abouttypedef:
In C and C++ programming languages, typedef is a keyword. It is used to give a data type an alias, with the purpose of making the source code easier to read and understand. It is often used to simplify the declaration of complex types, but it is also commonly seen in various lengths of integer data types, such as size_t and time_t.
Method 4: Use typedef to define the structure while giving the stu structure the alias STU, so that subsequent definitions can use STU directly without using struct stu.
typedef struct stu { char aa; short bb; int cc; } STU; STU stu1;
You will find that it is actually just using the typedef keyword to replace struct stu with STU. Of course, you can also use it like this:
struct stu stu1;
This definition loses the meaning of typedef, so it is not recommended.Method 5: When using typedef to define a structure, you can skip the first alias stu and directly add STU after it, using it the same way as above.
typedef struct { char aa; short bb; int cc; } STU; STU stu1;
Method 6: There is also a definition method that conforms to the syntax rules but is not meaningful.
typedef struct stu { char aa; short bb; int cc; }; struct stu stu1;
All these definition methods are considered syntax knowledge; if you still don’t understand, please review again.
Calculating Structure Size
Many people may not understand the size of structures. In actual programming, this is applied in many places, for example: when storing or copying structures, the issue of structure size will be involved.
1. Comparing the sizes of 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 no. The program test shows that sizeof(stu1)=6, sizeof(stu2)=4.2. Analyzing the reason for the difference Structure size calculations must follow the principle of byte alignment, generally satisfying three criteria:1) The address of the structure variable must be divisible by the size of its widest basic type member;2) The offset of each member relative to the structure’s starting address must be an integer multiple of the member’s size; if necessary, the compiler will add padding bytes between members;3) The total size of the structure must be an integer multiple of the size of its widest basic type member; if necessary, the compiler will add padding bytes after the last member; For the above structure stu1, the maximum byte is:2 bytes, the order is char->short->char:
| The first char occupies one byte | The extra byte is just a placeholder |
| short occupies exactly 2 bytes | |
| The second char also occupies 1 byte | The extra byte is just a placeholder |
For the above structure stu2, the maximum byte is:2 bytes, the order is char->char->short:
| The first char occupies one byte | The second char occupies one byte |
| short occupies exactly 2 bytes |
Through the above two tables, I believe you should understand that changing to a 4-byte int follows the same principle. Finally, I want to emphasize that learning programming cannot just be about reading books; you must practice coding to improve quickly.
———— END ————

● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorial”
● Selected Tutorials from Embedded Column
Follow the public account Reply “Add Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Click “Read the original text” to see more shares.