当前位置:网站首页>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 .
边栏推荐
- 一个悄然崛起的国产软件,低调又强大!
- Wechat applet realizes keyword highlighting
- 利用win7漏洞进行系统登录密码破解
- 较真儿学源码系列-InheritableThreadLocal(逐行源码带你分析作者思路)
- 毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
- 振弦采集模塊測量振弦傳感器的流程步驟
- Ffmpeg avframe to cv:: mat
- SwiftUI 4 新功能大全之 Toggle与 Mixed Toggle 多个绑定组件
- Wechat applet navigator has a shadow after clicking. Remove the shadow effect of navigator
- [exercise] HashSet
猜你喜欢

Actual combat of flutter - fast implementation of audio and video call application

再回顾集合容器

How to use console Log print text?

简单但现代的服务器仪表板Dashdot

Why has instagram changed from a content sharing platform to a marketing tool? How do independent sellers use this tool?

GC垃圾回收

JS proxy

Related concepts of cookies and sessions
![[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached](/img/50/926cc5bce83f8b195b3e2072b656bf.jpg)
[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached

EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
随机推荐
118. Yanghui triangle
What is the essential difference between Bi development and report development?
2022/5/23-2022/5/30
Anaconda installs the virtual environment to the specified path
新增订单如何防止重复提交
产品模块化设计的前世今生
开发那些事儿:EasyCVR平台添加播放地址鉴权功能
[exercise] HashSet
全国职业院校技能大赛网络安全“splunk“详细配置
The key to the success of digital transformation enterprises is to create value with data
Hls4ml entry method
Uni app product classification
Class loading mechanism
Process steps of vibrating wire acquisition module for measuring vibrating wire sensor
Audio and video, encoding and decoding related e-books, gadgets, packaged for free!
类加载机制
Uni app wechat applet one click login to obtain permission function
墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
Is Dao safe? Build finance encountered a malicious governance takeover and was looted!
List is divided into sets that meet and do not meet conditions (partitioningby)