当前位置:网站首页>一文读懂C语言中的结构体
一文读懂C语言中的结构体
2022-07-01 18:46:00 【嵌入式悦翔园】
1、什么是结构体
结构体是C语言中一种重要的数据类型,该数据类型由一组称为成员(或称为域,或称为元素)的不同数据组成,其中每个成员可以具有不同的类型。结构体通常用来表示类型不同但是又有关系的若干数据。其关键字为struct。
2、结构体的声明
我们通过关键字struct对结构体进行声明,具体格式如下:
struct book
{
char title[100];
char author[100];
char date[100];
}
我们通过上面的语句定义出了一本书的三个属性,分别是titleauthordate,且格式为char型(字符型)
注意:声明时也可以不完全声明,比如省略结构体标签。
3、结构体的引用
定义了结构体变量之后就可以在程序中对他进行引用,但是结构体变量的引用同一般变量引用不太一样,因为结构体变量中有多个不同类型变量,所以结构体变量不能整体引用,只能一个一个成员的引用。
1、只能分别单独引用他内部的成员。
//结构体变量名.成员名
book.title = "三国演义";
book.author = "罗贯中";
book.date = "2006";
2、如果结构体类型中的成员也是一个结构体类型,则要使用若干个".",一级一级的找下去。因为只能对最低级的成员进行操作。其实结构体就是对数据的一种封装,当结构体成员也是结构体时,完全可以将结构体成员释放出来。
封装前:
struct age
{
int year;
int month;
int day;
}
struct student
{
char name[20];
int num;
struct age birthday;
float score;
}
封装后:
struct student
{
char name[20];
int num;
int year;
int month;
int day;
float score;
}
3、可以引用结构体变量成员的地址,也可以引用结构体变量的地址。如&student1.num 和 &student1,前者标志student1.num这个成员在内存中的首地址,后者表示结构体变量student1在内存中的首地址。
在C语言中,结构体变量的首地址就是结构体第一个成员的首地址。所以&student1就等价于第一个成员name的首地址,而name是一个数组,数组名表示的就是数组的首地址。但是要注意的是,他们等价于他们表示的是同一个内存空间的地址,但他们的类型是不同的。
4、结构体作用
1、结构体和其他类型基础数据类型一样,例如int类型、char类型,只不过结构体可以做成你想要的数据类型,以方便日后的使用。
2、结构体指针作函数入口参数,提高程序的可扩展性。
5、结构体的大小与内存对齐
结构体的大小不是结构体元素单纯相加就行的,因为我们主流的计算机使用的都是32bit字长的CPU,对这类型的CPU取4个字节的数要比取一个字节要高效,也更方便。
所以在结构体中每个成员的首地址都是4的整数倍的话,取数据元素时就会相对更高效,这就是内存对齐的由来。
在每个特定平台上的编译器都有自己的默认“对齐系数”(也叫对齐模数)。程序员可以通过预编译命令#pragma pack(n),n=1,2,4,8,16来改变这一系数,其中的n就是你要指定的“对齐系数”。
6、结构体对齐规则
1、数据成员对齐规则:结构(struct)(或联合(union))的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员的对齐按照#pragma pack指定的数值和这个数据成员自身长度中,比较小的那个进行。
2、结构(或联合)的整体对齐规则:在数据成员完成各自对齐之后,结构(或联合)本身也要进行对齐,对齐将按照#pragma pack指定的数值和结构(或联合)最大数据成员长度中,比较小的那个进行。
3、结合1、2可推断:当#pragma pack的n值等于或超过所有数据成员长度的时候,这个n值的大小将不产生任何效果。
边栏推荐
- DDR4 test-2
- IPv4地址、子网掩码、网关
- sql查询去重统计的方法总结
- 商业智能BI开发和报表开发有什么本质区别?
- Extensive reading of the paper [film: visual reasoning with a general condition layer]
- 音视频、编解码相关电子书、小工具,打包奉送!
- How to configure webrtc video streaming format for easygbs, a new version of national standard gb28181 video platform?
- torch.nn.functional.interpolate函数
- How to solve the problem of splash screen when the main and sub code streams of easygbs are h.265?
- After studying 11 kinds of real-time chat software, I found that they all have these functions
猜你喜欢

How to solve the problem of splash screen when the main and sub code streams of easygbs are h.265?

nacos启动失败问题解决与总结

Basic use of MySQL

Case sharing: basic networking configuration of QinQ

AAAI2020: Real-time Scene Text Detection with Differentiable Binarization

Les canaux de culture intensive s'efforcent de développer Fu Xin et Wei Shi jiajie pour organiser une conférence de formation sur les nouveaux produits

Parallelism, concurrency and life cycle of threads

ubuntu14安装MySQL并配置root账户本地与远程访问

Ubuntu14 install MySQL and configure root account local and remote access

Detailed explanation of JUnit unit test framework
随机推荐
How to redraw the header of CListCtrl in MFC
Compile ffmpeg source code with msys+vs2019 under win10
Les canaux de culture intensive s'efforcent de développer Fu Xin et Wei Shi jiajie pour organiser une conférence de formation sur les nouveaux produits
[SQL optimization] the difference between with as and temporary tables
EasyGBS网络不稳定情况下重复请求视频拉流问题的优化
大厂音视频职位面试题目--今日头条
torch.nn.functional.interpolate函数
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
How to solve the problem of splash screen when the main and sub code streams of easygbs are h.265?
ffmpeg 音频相关命令
Shell advanced
Live HLS protocol
The use of subplot function in MATLAB
CMU AI PhD 第一年总结
博途V16 获取系统时间转换成字符串
IPv4 address, subnet mask, gateway
win10下使用msys+vs2019编译ffmpeg源码
Contos 7 set up SFTP to create users, user groups, and delete users
Axure does not display catalogs
After studying 11 kinds of real-time chat software, I found that they all have these functions