当前位置:网站首页>Understand the structure in C language in one article
Understand the structure in C language in one article
2022-07-01 19:54:00 【Embedded Yuexiang Garden】
1、 What is a structure
The structure is C Language An important data type in , This data type consists of a group called members ( Or domain , Or element ) Different data components , Each of these members can have a different type . Structures are usually used to represent data of different types but related . The keyword is struct.
2、 Declaration of a structure
We use keywords struct Declare structure , The specific format is as follows :
struct book
{
char title[100];
char author[100];
char date[100];
}
We have defined three attributes of a book through the above statement , Namely titleauthordate, And the format is char type ( Character )
Be careful : The declaration can also be incomplete , For example, omit the structure label .
3、 Reference to structure
After defining the structure variable, you can reference it in the program , But the reference of structural variables is different from that of general variables , Because there are many different types of variables in structural variables , Therefore, structural variables cannot be referenced as a whole , Only one member can be referenced .
1、 You can only quote his internal members separately .
// Structure variable name . Member name
book.title = " The romance of The Three Kingdoms ";
book.author = " Luo Guanzhong ";
book.date = "2006";
2、 If the member in the structure type is also a structure type , You need to use several ".", Go down level by level . Because only the lowest level members can be operated . In fact, a structure is a kind of encapsulation of data , When the structure member is also a structure , Structure members can be released completely .
Before packaging :
struct age
{
int year;
int month;
int day;
}
struct student
{
char name[20];
int num;
struct age birthday;
float score;
}
After encapsulation :
struct student
{
char name[20];
int num;
int year;
int month;
int day;
float score;
}
3、 You can reference Structural variable Members of the Address , You can also quote Structural variable The address of . Such as &student1.num and &student1, Former mark student1.num The first address of this member in memory , The latter represents structural variables student1 First address in memory .
stay C In language , The first address of the structure variable is the first address of the first member of the structure . therefore &student1 It is equivalent to the first member name The first address , and name Is an array , The array name represents the first address of the array . But here's the thing , They are equivalent to They represent the address of the same memory space , But their types are different .
4、 Structure action
1、 Structs are the same as other types of underlying data types , for example int type 、char type , It's just that the structure can be made into the data type you want , For future use .
2、 Structure pointer is used as function entry parameter , Improve the scalability of the program .
5、 The size of the structure is aligned with the memory
The size of the structure is not simply the sum of the structure elements , Because our mainstream computers use 32bit Word length CPU, For this type of CPU take 4 The number of bytes is more efficient than taking one byte , And more convenient .
So the first address of each member in the structure is 4 An integer multiple of , When fetching data elements, it will be relatively more efficient , This is the origin of memory alignment .
Compilers on each specific platform have their own default “ Alignment factor ”( Also called alignment modulus ). Programmers can use precompile commands #pragma pack(n),n=1,2,4,8,16 To change this coefficient , Among them n That's what you're going to specify “ Alignment factor ”.
6、 Structure alignment rules
1、 Data member alignment rules : structure (struct)( Or in association with (union)) Data members of , The first data member is placed in offset by 0 The place of , In the future, the alignment of each data member is in accordance with #pragma pack The specified value and the length of the data member itself , The smaller one is going to .
2、 structure ( Or in association with ) The overall alignment rule of : After the data members are aligned , structure ( Or in association with ) Alignment itself , The alignment will follow #pragma pack Specified values and structures ( Or in association with ) In the maximum data member length , The smaller one is going to .
3、 combination 1、2 It can be inferred that : When #pragma pack Of n When the value equals or exceeds the length of all data members , This n The size of the value will have no effect .
边栏推荐
- 有意思了!数据库也搞Serverless!
- Interview question 16.16 Partial sorting - Double finger needling
- 自定义插入页面标签以及实现类似通讯录的首字母搜索
- Nat penetration of gb28181
- list大集合等比分割成多个小list集合
- Interview questions shared in today's group
- Compile ffmpeg source code with msys+vs2019 under win10
- Optimization of video streaming with repeated requests in the case of unstable easygbs network
- 2022/5/23-2022/5/30
- Test self-study people must see: how to find test items in software testing?
猜你喜欢

实例讲解将Graph Explorer搬上JupyterLab

HLS4ML进入方法

Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results

Regular expression =regex=regular expression

EasyCVR集群视频广场页面切换时,请求流未能终止的问题优化
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

【多线程】锁策略

Related concepts of cookies and sessions

118. Yanghui triangle

P2433 【深基1-2】小学数学 N 合一
随机推荐
STC 32位8051单片机开发实例教程 三 程序编译设置与下载
开发那些事儿:EasyCVR平台添加播放地址鉴权功能
Uni app wechat applet one click login to obtain permission function
1592 例题1 国王(Sgu223 LOJ10170 LUOGU1896 提高+/省选-) 暴力思考 状压DP 01背包
Hls4ml/vivado HLS error reporting solution
Define dichotomy lookup
Hls4ml entry method
[untitled]
振弦采集模塊測量振弦傳感器的流程步驟
Compile ffmpeg source code with msys+vs2019 under win10
2022/6/8-2022/6/12
118. Yanghui triangle
JVM memory model
IPv4 address, subnet mask, gateway
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
The key to the success of digital transformation enterprises is to create value with data
科技T3国产平台!成功搭载“翼辉国产实时系统SylixOS”
List is divided into sets that meet and do not meet conditions (partitioningby)
使用 Kibana Timelion 进行时间序列分析
[research materials] national second-hand housing market monthly report January 2022 - Download attached