当前位置:网站首页>Structure knowledge points all in
Structure knowledge points all in
2022-06-12 21:05:00 【Bald and weak】
Structure :
Definition :
struct Type name
{
( Member list \ attribute )
member 1;
member 2;
.....
}; // The key is : What information is needed
eg:
struct Student
{
char name[20];
int age;
};// Definition complete , Its function is the same as that of the built-in type , Type design does not take up memory space
Use :
Define variables by type , Then initialize the data in the variable one by one according to the definition order
eg:
struct Student student = { " Li Si ",14};
struct Student student1;// If it's a local variable ( It's written inside the function )-> uninitialized , The member contents are random values , Error will be reported in compilation ; If it's a global variable ( Write outside function )-> uninitialized , The compiler initializes the member list by default int,short Those are 0,char The type is \0, Compile without error
Example solution :
struct Student a;// utilize struct Student The data type defines a variable a;
struct Student b = { " Cao Cao ",20};
b.age = 21;// The common variables of the structure are passed through “.” Visit its members
(1) modify b My name is Liu Bei
error :b.name=” Liu bei ”// String assignment to array... Is not allowed , You should use a loop , It has nothing to do with the structure
correct :strcpy(b.name, " Liu bei ");
(2) Output b The data of : Name and age
printf(" % s, % d", b.name, b.age);
(3) take a Change it to “ king of Wu in the Three Kingdoms Era ”,18
strcpy(a.name, " king of Wu in the Three Kingdoms Era ");
a.age = 18;
Simplify the use :
1.struct Type name
{ } Alias ;
2.typedef struct Type name Alias ;
Nested structure :
It can be used in the structure Previously stated Type of structure
eg:
struct Date
{
int year;
int month;
int day;
}Date;
struct Student
{
const char* name;
int age;
Date birthday;
};
int main()
{
struct Student s1 = { " Li Si ",14,{2001.9.18}};
}
size :
test :printf(“%d”,sizeof(struct Student));
computing method : Related to memory alignment , The following is written .
Array of structs :
eg:struct Studeng arr[]={ {},{}...};
visit :
arr[i].name,arr++
Member visit :
Variable name . Structural variable ‘.’ Member accessor
eg:student.name
How to understand byte alignment problem :
- The basic unit of memory is bytes , In theory , Variables can be accessed at any address , But actually CPU It does not read memory byte by byte , But rather 2,4,8 Read and write multiple of memory block , Therefore, the address of basic data type is limited , Then it requires that various data types be arranged in space according to certain rules .
- Some platforms start at my address every time , And if it's stored at the beginning of the odd address , Need 2 All read cycles , And the high and low bytes of the result read out twice can be pieced together to get the 32bit data . Obviously, there is a lot of decrease in reading efficiency .
- Due to different platforms, alignment may be different . In this way , The same structure may have different sizes on different platforms , Unconsciously , The data sent to each other may be out of order , It even caused serious problems .
How to calculate the size of a structure :
- The first address of the structure variable address , Must be in a struct variable “ The maximum number of bytes occupied by basic data type members ” Integer multiple .
eg:struct Student
{
char name[20]; //1*20=20 byte
int age; //4 byte
};
because 20 Is the largest byte in the structure ,so The first address of a struct variable must be 20 Multiple . Pictured
-> The multiple is 0
2. The offset of each member in the structure variable from the first address of the structure , Are integer multiples of the bytes occupied by the basic data type of the member .
3. The total size of the structure variable , Is in the structure variable “ Maximum Basic data type Number of bytes occupied by members ” The integer of
times ( Be careful :struct And array are not basic data types )
Give an example of : Allocate the bytes shown in the figure in memory
(1)
Explain :
char cha:1 byte ,char chb:1 byte ,int ia:4 byte , At least 4 Position of , So two bytes are wasted .1+1+2+4=8 yes 4 Multiple , Therefore, the byte size of the structure is 8
(2)
Explain :
cha:1,da:8 Need to waste in front 7 Bytes ,chb:1.8+8+1=17. Minimum 24.
(3)
Explain :
ch:3,ia:4, Waste one in front ,4+4=8, yes 4 Multiple , Then for 8.
(4) Nested structure analysis :
Explain :10+8+12=30,4*8=32 waste 2 Bytes .
32+8=40, The basic data type is double:8 byte ,40 yes 8 Multiple , The structure byte size is 40.
Instruction aligner :
Preprocessing instruction #pragma pack(n), You can change the default number ,n The values for 1,2,4,8,16.
If #pramga pack(n) Medium n Larger than the number of bytes occupied by any one of the structure members , Then n Invalid value . The compiler will select the number of bytes of the largest data member in the structure as the benchmark for its .
eg:
The number of bytes is 2,a Read 1 Time , waste 1 Space ,b Read twice ,c Read 1 Time , waste 1 Space .
The number of bytes is 4,a Read 1 Time , waste 3 Space ,b Read 1 Time ,c Read 1 Time , waste 3 Space .
Structure pointer :
Definition :
typedef struct Student* stu;
perhaps
struct Student
{
char name[20];
int age;
}*stu;
Use :
Define pointer variables by type , And then give it a direction .
eg:
Student s1 = { " Zhang San ",20 };
typedef struct Student* stu;
stu S=&s1;
adopt S Output s1 The content of .
eg:
printf(“%c,%d”,S->name,S->age);
Member visit :
1.(* Variable name ). Structural variable ‘.’ Member accessor
(*S).age; Must have parentheses , because “.” Has a higher priority than “*”
2. Variable name -> Structural variable ‘->’ Pointer
S->age;
边栏推荐
- 金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试
- Double carbon in every direction: green demand and competition focus in the calculation from the east to the West
- USB机械键盘改蓝牙键盘
- String Basics
- Simplest ALV template
- Summary of machine learning materials
- A blog written clearly by vit
- new做了哪几件事
- 入行5年从10k的功能测试到年薪40w的测试开发,花7天时间整理的超全学习路线
- 同时做测试,别人已经年薪20w起,为什么你还在为达到月薪10k而努力?
猜你喜欢

Data visualization - biaxial comparison effect

Introduction to scala basic grammar (III) various operators in Scala

leetcode:210. 課程錶 II

Listener in JSP

Typescript definition type: type 'timeout' cannot be assigned to type 'number';

torch. nn. Linear() function

JSP中的监听器

新品发布丨竣达智能综合环境监测终端

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

Introduction to the characteristics of balancer decentralized exchange market capitalization robot
随机推荐
2022年春招,测试工程师全套面试攻略,一篇吃透全部技术栈(全是干货)
UVa11991 Easy Problem from Rujia Liu
How to improve communication efficiency during home office | community essay solicitation
Pytorch how to set random number seed to make the result repeatable
How to implement overloading in JS
New product release Junda intelligent integrated environmental monitoring terminal
Lake shore PT-100 platinum resistance temperature sensor
金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试
Lombok package is successfully installed, but the runtime prompts that get, set method and constructor solution cannot be found
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)
InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY EB3E94ADBE1229CF
冒泡排序
Large and small end conversion
Social metauniverse: start from redefining yourself
初步了解认识正则表达式(Regex)
Access control system based on RFID
Can tonghuashun open an account? Can tonghuashun directly open the security of securities companies on the app? How to open an account online when buying stocks
同花顺能开户吗,在APP上可以直接开通券商安全吗 ,买股票怎么网上开户
Data visualization - biaxial comparison effect
作用域和作用域链