当前位置:网站首页>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 title
author
date
, 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 .
边栏推荐
- Mysql查询结果去除换行
- CMU AI PhD first year summary
- Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
- Define dichotomy lookup
- Test self-study people must see: how to find test items in software testing?
- Hls4ml entry method
- SQL 入门计划-1-选择
- What is the essential difference between Bi development and report development?
- Why must we move from Devops to bizdevops?
- MySQL signale une erreur can 't create table' demo01. TB Étudiant '(errno: 150)
猜你喜欢
Why must we move from Devops to bizdevops?
3D全景模型展示可视化技术演示
Oracle physical architecture
Uni app wechat applet one click login to obtain permission function
1592 例题1 国王(Sgu223 LOJ10170 LUOGU1896 提高+/省选-) 暴力思考 状压DP 01背包
一个悄然崛起的国产软件,低调又强大!
[research materials] national second-hand housing market monthly report January 2022 - Download attached
servlet知识点
Process steps of vibrating wire acquisition module for measuring vibrating wire sensor
Example explanation: move graph explorer to jupyterlab
随机推荐
面试题篇一
windows环境 redis安装和启动(后台启动)
PowerDesigner设计Name和Comment 替换
振弦采集模塊測量振弦傳感器的流程步驟
Linux下安装Redis,并配置环境
Stack Overflow 2022 开发者调查:行业走向何方?
一个程序员如何快速成长
IPv4 address, subnet mask, gateway
Wechat applet navigator has a shadow after clicking. Remove the shadow effect of navigator
List is divided into sets that meet and do not meet conditions (partitioningby)
Process steps of vibrating wire acquisition module for measuring vibrating wire sensor
Procédure de mesure du capteur d'accord vibrant par le module d'acquisition d'accord vibrant
Review the collection container again
js三元表达式复杂条件判断
JVM内存模型
[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached
类加载机制
The key to the success of digital transformation enterprises is to create value with data
[research materials] national second-hand housing market monthly report January 2022 - Download attached
Hls4ml/vivado HLS error reporting solution