当前位置:网站首页>结构体知识点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;
边栏推荐
- Can tonghuashun open an account? Is it safe to open an account in tonghuashun
- Nexus3搭建本地仓库
- Vs2017 environmental issues
- The salted fish has been transmitted for 5W times, and the latest spring recruit face-to-face test questions of bytes have been leaked
- JSON file handles object Tags
- #886 Possible Bipartition
- 阿里前辈给我推荐的软件测试人员必读书籍(附电子书),让我受益匪浅...
- Lombok package is successfully installed, but the runtime prompts that get, set method and constructor solution cannot be found
- Before job hopping, Jin San made up the interview questions. Jin San successfully landed at Tencent and got a 30K test offer
- Research Report on hydraulic solenoid valve industry - market status analysis and development prospect forecast
猜你喜欢

My way of programming

A Zhu and Xu Baobao's high-rise game - difference

Successful transition from self-study test halfway, 10K for the first test

Data visualization diagram microblog forwarding diagram

The required books for software testers (with e-books) recommended by senior Ali have benefited me a lot

Lombok package is successfully installed, but the runtime prompts that get, set method and constructor solution cannot be found

Lake shore PT-100 platinum resistance temperature sensor

Scala基础语法入门(三)Scala中的各种运算符

Solution of multi machine room dynamic loop status network touch screen monitoring

typeScript的定义类型:不能将类型“Timeout”分配给类型“number”;
随机推荐
Research Report on hydraulic solenoid valve industry - market status analysis and development prospect forecast
What are the disadvantages of bone conduction earphones? Analysis of advantages and disadvantages of bone conduction earphones
Introduction to the characteristics of balancer decentralized exchange market capitalization robot
Gather function in pytorch_
Lake shore PT-100 platinum resistance temperature sensor
torch. clamp_ min_ method
初步了解認識正則錶達式(Regex)
Introduction to scala basic grammar (III) various operators in Scala
[tutorial] detailed explanation of the page rules parameter settings of cloudflare CDN
Market trend report, technical innovation and market forecast of hydraulic chain hoist in China
Summary of machine learning materials
Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"
leetcode:207. Class Schedule Card
Integrated monitoring solution for power environment of small and medium-sized computer rooms
机器学习资料汇总
Nexus3 build local warehouse
最简单ALV模板
Research Report on market supply and demand and strategy of China's hydraulic hammer industry
Inrelease: the following signature cannot be verified because there is no public key: no_ PUBKEY EB3E94ADBE1229CF
Design and practice of Hudi bucket index in byte skipping