当前位置:网站首页>Notes on advanced points of C language 4
Notes on advanced points of C language 4
2022-04-23 06:42:00 【~Ha】
Catalog
- 5、 ... and 、 Structure
1、 Definitions and concepts
2、 Array of structs
3、 Structure pointer
4、 Structure memory allocation
5、 Bit segment ( Bit field )
- 6、 ... and 、 Shared body
- 7、 ... and 、 Enumeration type
- 5、 ... and 、 Structure
1、 Definitions and concepts
struct Structure type name
{
Member list
};
You can explain first and then define , You can also define... Before the semicolon
Define structure variables before semicolons , At the same time, omit Structure type name , Then you can no longer define this structure variable , At this point, the definition is the only , It is often used for global variables that are used a limited number of times
typedef struct ( Structure type name is optional )
{
Member list
} Type the name ; Variables cannot be defined before semicolons
To define a variable, just Type name plus variable name
Variable name plus dot plus member name for assignment
When a structure is described, its member list can make another structure , Multi level references can be used
Structural variables of the same type can be assigned to each other directly
2、 Array of structs
Structure array is an array , A collection of several structural variables of the same type
struct Structure type name Array name [ Element number ];
3、 Structure pointer
The structure pointer is defined and assigned in the same way as ordinary variables
The overall reference of structure is the same as that of ordinary variables
But it's different when referring to structure members , You need brackets ,. The priority ratio * high ,(*ps).num
Bracketed trouble , Available pointing operators (->) Instead of ,ps->num
When there is nesting , When referencing a member of an inner structure , The first layer uses the pointing operator, and the second layer uses the dot operator
4、 Structure memory allocation
#pragma pack (value) The specified alignment value for value.
value Can only be : 1 2 4 8 etc.
Byte alignment , The current member is value Integer multiples remain unchanged ; The current member is less than value, But with the latter member, it happens to be value The integral multiple of is also unchanged ; The current member is less than value, Plus the latter member is also less than value The integral multiple of is supplemented by value Integer multiple .
You can use sizeof Measure the space occupied
5、 Bit segment ( Bit field )
struct packed_data
{
unsigned int a:2;
unsigned int b:6;
unsigned int c:3;
unsigned int :1; // Meaningless paragraph
unsigned int d:4;
unsigned int i;
} data;
The number after the colon indicates how many digits , Cannot take address for bit segment member
Do not exceed the range defined by the bit segment when assigning , If the assignment is out of range, it will be intercepted
Meaningless bit segments can be defined , Simply take up space , To ensure that the following members occupy the required bits
In the same structure , Member variables can include both bit segments and ordinary member variables
- 6、 ... and 、 Shared body
The common body is similar to the structure , It's also a construction type of data structure
The definition method of common body is also very similar to that of structure , take struct Change to union that will do , Other methods of use are the same , But no bit segment is allowed
When performing some algorithms , Several different types of variables need to be stored in the same memory unit , The spaces used by several variables overlap each other
The size of a community is the size of its member with the largest memory length
Only one member of a community is valid at a time , It is often used in the sending process of network protocol
- 7、 ... and 、 Enumeration type
List the values of variables one by one , The value of a variable is limited to the values listed
enum Enumeration type name
{
Enumerate the list of values , ( Members are separated by commas )
};
Also available typedef
Where enumeration is used, macro definition can be used instead of , Not vice versa
Enumeration members can be any content , Its actual value is an integer , The default from the 0 Began to increase , You can also change the integer value corresponding to the member by assigning a value to the member
Definition of enumeration variables :enum Enumeration type name Enumerate variable names
版权声明
本文为[~Ha]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230548245373.html
边栏推荐
猜你喜欢
【UDS统一诊断服务】一、诊断概述(2)— 主要诊断协议(K线和CAN)
cv_bridge 与opencv 版本不匹配的解决
大学概率论与数理统计知识点详细整理
[ThreadX] ThreadX source code reading plan (I)
[UDS unified diagnosis service] IV. typical diagnosis service (3) - read fault information function unit (storage data transmission function unit)
[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit
[UDS unified diagnostic service] III. application layer protocol (1)
Cross domain issues - allow origin header contains multiple values but only one is allowed
【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (1)
【UDS统一诊断服务】一、诊断概述(1)— 诊断概述
随机推荐
静态成员
Matching between class template with default template argument and template parameter
逻辑回归原理及代码实现
搭建jpress个人博客
GDB debugger installation and use
C#【文件操作篇】PDF文件和图片互相转换
sqlite3加密版
PHP junior programmers, take orders and earn extra money
【UDS统一诊断服务】四、诊断典型服务(2)— 数据传输功能单元
进程管理命令
undefined reference to `Nabo::NearestNeighbourSearch
圆整 round 的一点点小细节
C语言输入和输出(printf和scanf函数、putchar和getchar函数)
[opencv] use filestorage to read and write eigenvectors
[UDS unified diagnosis service] i. diagnosis overview (1) - diagnosis overview
[ThreadX] h743 + ThreadX + Filex migration record
Rust:单元测试(cargo test )的时候显示 println 的输出信息
友元函数,友元类,类模板
深蓝学院激光slam理论与实践 -第二章(里程计标定)作业
ArcGIS表转EXCEL超出上限转换失败