当前位置:网站首页>结构体知识点all in
结构体知识点all in
2022-06-12 21:02:00 【又秃又弱】
结构体:
定义:
struct类型名
{
(成员列表\属性)
成员1;
成员2;
.....
}; //重点是:需要哪些信息
eg:
struct Student
{
char name[20];
int age;
};//定义完成,其作用和内置类型一样,类型的设计不占用内存空间
使用:
通过类型来定义变量,再对变量内的数据按照定义顺序挨个初始化
eg:
struct Student student = { "李四",14};
struct Student student1;//如果是局部变量(写在函数内部)->未初始化,成员内容为随机值,编译会报错;如果是全局变量(写在函数外部)->未初始化,编译器给成员列表默认值初始化int,short那些都是0,char类型为\0,编译不会报错
例题求解:
struct Student a;//利用struct Student数据类型定义一个变量a;
struct Student b = { "曹操",20};
b.age = 21;//结构体的普通变量通过“.”访问其成员
(1)修改b的名字为刘备
错误:b.name=”刘备”//不允许字符串赋值给数组,应该用循环,跟结构体没关系
正确:strcpy(b.name, "刘备");
(2)输出b的数据:名字和年龄
printf(" % s, % d", b.name, b.age);
(3)将a改为“孙权”,18
strcpy(a.name, "孙权");
a.age = 18;
简化使用:
1.struct类型名
{ }别名;
2.typedef struct 类型名 别名;
结构体嵌套:
结构体内可以使用之前声明过的结构体类型
eg:
struct Date
{
int year;
int month;
int day;
}Date;
struct Student
{
const char* name;
int age;
Date birthday;
};
int main()
{
struct Student s1 = { "李四",14,{2001.9.18}};
}
大小:
检验:printf(“%d”,sizeof(struct Student));
计算方法:与内存对齐有关,下面有写到。
结构体数组:
eg:struct Studeng arr[]={ {},{}...};
访问:
arr[i].name,arr++
成员访问:
变量名称.结构体变量 ‘.’成员访问符
eg:student.name
如何理解字节对齐问题:
- 内存的基本单位是字节,理论上来说,可以任意地址访问变量,但实际上CPU并不是逐个字节的进行内存读取,而是以2,4,8的倍数的字节块来读写内存,因此对基本数据类型的地址做出了限制,那么就要求各种数据类型按照一定的规则在空间上排列。
- 有些平台每次都是从偶地址开始,而如果存放在奇地址开始的地方,就需要2个都读周期,并对两次读出的结果高低字节进行拼凑才能得到该32bit数据。显然在读取效率上下降很多。
- 由于不同平台对齐方式可能不同。如此一来,同样的结构在不同的平台其大小可能不同,在无意识的情况下,互相发送的数据可能出现错乱,甚至引发严重的问题。
如何计算结构体的大小:
- 结构体变量地址的首地址,必须是结构体变量中的“最大基本数据类型成员所占字节数”的整数倍。
eg:struct Student
{
char name[20]; //1*20=20字节
int age; //4字节
};
因为20为结构体内最大字节,so结构体变量首地址必须是20的倍数。如图
->倍数为0
2.结构体变量中的每个成员相对于结构体首地址的偏移量,都是该成员基本数据类型所占字节的整数倍。
3.结构体变量的总大小,为结构体变量中“最大基本数据类型成员所占字节数”的整数
倍(注意:struct和数组都不是基本数据类型)
举例分析:将如图字节在内存中分配
(1)
解:char cha:1字节,char chb:1字节,int ia:4字节,最起码要在4的位置放,因此浪费两个字节。1+1+2+4=8是4的倍数,因此结构体的字节大小为8
(2)
解:cha:1,da:8需要前面浪费7个字节,chb:1。8+8+1=17。最小24。
(3)
解:ch:3,ia:4,前面浪费一个,4+4=8,是4的倍数,则为8。
(4)嵌套结构体分析:
解:10+8+12=30,4*8=32浪费2个字节。
32+8=40,基本数据类型为double:8字节,40是8的倍数,结构体字节大小为40。
指令对齐符:
预处理指令#pragma pack(n),可以改变默认对其数,n的取值为1,2,4,8,16。
如果#pramga pack(n)中的n大于结构体成员中任何一个成员所占用的字节数,则该n值无效。编译器会选取结构体中最大数据成员的字节数为基准进行对其。
eg:字节对其数为2,a读取1次,浪费1个空间,b读取两次,c读取1次,浪费1个空间。
字节对其数为4,a读取1次,浪费3个空间,b读取1次,c读取1次,浪费3个空间。
结构体指针:
定义:
typedef struct Student* stu;
或者
struct Student
{
char name[20];
int age;
}*stu;
使用:
通过类型来定义指针变量,再对其赋与指向。
eg:
Student s1 = { "张三",20 };
typedef struct Student* stu;
stu S=&s1;
通过S输出s1的内容。
eg:
printf(“%c,%d”,S->name,S->age);
成员访问:
1.(*变量名称).结构体变量 ‘.’成员访问符
(*S).age; 必须有括号,因为“.”的优先级高于“*”
2.变量名称->结构体变量 ‘->’指向符
S->age;
边栏推荐
- Social metauniverse: start from redefining yourself
- Algorinote_ 2_ Main theorem and Akra bazzi theorem
- torch. Finfo function
- New product release Junda intelligent integrated environmental monitoring terminal
- How to download putty using alicloud image?
- Double carbon in every direction: green demand and competition focus in the calculation from the east to the West
- typeScript的定义类型:不能将类型“Timeout”分配给类型“number”;
- Design and practice of Hudi bucket index in byte skipping
- Data visualization diagram microblog forwarding diagram
- Inrelease: the following signature cannot be verified because there is no public key: no_ PUBKEY EB3E94ADBE1229CF
猜你喜欢
Scope and scope chain
Teambition 协作应用心得分享|社区征文
Mxnet record IO details
#113 Path Sum II
In the spring recruitment of 2022, the test engineer will have a full set of interview strategies to thoroughly understand all the technical stacks (all dry goods)
没有学历,自学软件测试,找到一份月入过万的测试工作真的有可能吗?
nn. PReLU(planes)
Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"
My way of programming
#113 Path Sum II
随机推荐
Pytorch how to set random number seed to make the result repeatable
Allegro Xile technology, a developer of distributed cloud services, received millions of dollars of angel round financing and was independently invested by Yaotu capital
Go -- monitor file changes
How can the openwrt package manager image be switched to an alicloud source?
Scala基础语法入门(三)Scala中的各种运算符
Inrelease: the following signature cannot be verified because there is no public key: no_ PUBKEY EB3E94ADBE1229CF
What did new do
MySQL field truncation principle and source code analysis
At the same time, do the test. Others have been paid 20W a year. Why are you still working hard to reach 10K a month?
JSON file handles object Tags
Gather function in pytorch_
Data visualization - biaxial comparison effect
Solve the cvxpy error the solver GLPK_ MI is not installed
Research Report on market supply and demand and strategy of hydraulic operating table industry in China
Can tonghuashun open an account? Is it safe to open an account in tonghuashun
lintcode:127 · 拓扑排序
torch. unique()
Integrated monitoring solution for power environment of small and medium-sized computer rooms
Algorinote_2_主定理与 Akra-Bazzi 定理
Research Report on hydraulic solenoid valve industry - market status analysis and development prospect forecast