当前位置:网站首页>User defined data type - structure
User defined data type - structure
2022-06-25 14:57:00 【--988】
Custom data types
Structure
enumeration
Consortium
char,short,int long,float,double Built in type ——c The language has its own data types .
Structure declaration :
struct tag
{
member-list Member list
};variable-list Variable list
Declare a student type , Create student variables by student type ( object )
struct stu
{
char name[20];
char tale[12];
char sex[10];
int age;
}s4,s5,s6;// You can't lose the semicolon --- Global variables
struct stu s3;//--- Global variables
int main()
{
struct stu s1;//--- local variable
struct stu s2;//--- local variable
return 0;
}
Anonymous struct type :
struct
{
int a;
char b;
}x;//x Can't save
The compiler treats two struct declarations as two completely different types , therefore p=&x It's illegal.
data structure : The storage structure of data in memory –《 Linked list 》

typedef struct Node//Node Self reference cannot be omitted
{
int data;
struct Node *next;// Find variables of the same type
}Node;
int main()
{
struct Node n1;
Node n2;
return 0;
}
Structure alignment rules :
- The first member is offset from the structure variable by 0 The address of .
- Other member variables should be aligned to a certain number ( Align numbers ) An integral multiple of the address of .
- The total size of the structure is the maximum number of alignments ( Each member variable has an alignment number ) Integer multiple .
- If the structure is nested , Align nested structures to their maximum number of alignments ( The number of alignments with nested structures ) Integer multiple .
visual The default value in is 8
Align numbers = The compiler defaults to an alignment number and the smaller value of the member size
#pragma pack(4)// Set the default alignment digit 4
struct S
{
char c1;//1 Bytes
double d;//8 Bytes
};
#pragma park()// Unset the default number of alignments
C Language offsetof function : The return value of this macro is of type size-t, The offset value of this type member , Such as (printf"%d\n",offsetof(struct s,i));
The structure parameters shall be addressed as much as possible :
When a function passes parameters , The parameter is stack pressing , There will be time and space overhead . If you pass a structure object , The structure is too large , The system overhead of parameter stack pressing is relatively large , So it leads to performance degradation .
struct S
{
int a;
char c;
double d;
};
void Init(struct S tmp)
{
tmp.a=100;
tmp.c='w';
tmp.d=3.14;
}
void Print1(struct Stmp)
{
printf("%d %s %lf\n",tmp.a,tmp.c,tmp.d);
}
void Print 2(const struct S* ps)// Can be in ps Before to add const, Still address , But it cannot be changed S Value
{
printf("%d %c %lf\n",ps->a,ps->c,ps->d);
}
int main()
{
struct S s={
0};
Init (&s);// Must be address passing , Otherwise tmp Change will not affect s Change in value
Print 1(s);// Memory doesn't matter
Print 2(&s);
return 0;
}
边栏推荐
- [try to hack] vulhub shooting range construction
- Kubernetes 理解kubectl/调试
- 移除区间(贪心)
- H5 page graying source code, ie compatible (elegant downgrade provides download browser link)
- Installing QT plug-in in Visual Studio
- 15 -- 最接近原点的 K 个点
- 分饼干问题
- Output 0 ~ n digits and output multiple times
- Using Sphinx to automatically generate API documents from py source files
- ffmpeg protocol concat 进行ts流合并视频的时间戳计算及其音画同步方式一点浅析
猜你喜欢

搭建极简GB28181 网守和网关服务器,建立AI推理和3d服务场景,然后开源代码(一)

【Try to Hack】vulnhub DC1

How to make GIF animation online? Try this GIF online production tool

从0到1完全掌握 XSS

JS to add elements to the header, or tail of an array

JS select all exercise

Judging the number of leap years from 1 to N years

Gif动图如何裁剪?收下这个图片在线裁剪工具

Design and implementation of thread pool
![[try to hack] vulhub shooting range construction](/img/fc/6057b6dec9b51894140453e5422176.png)
[try to hack] vulhub shooting range construction
随机推荐
Get the parameters in the URL and the interchange between parameters and objects
2022年广东高考分数线出炉,一个几家欢喜几家愁
Flexible layout (display:flex;) Attribute details
定位position(5种方式)
Master XSS completely from 0 to 1
JS select all exercise
‘make_ unique’ is not a member of ‘std’
关于win10 版本kicad 卡死的问题, 版本6.x
New title of PTA
Why should the coroutine be set to non blocking IO
使用sphinx根据py源文件自动生成API文档
Using Sphinx to automatically generate API documents from py source files
NBD Network Block Device
Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment
Compile Caffe's project using cmake
JGG | 河北大学杜会龙组综述植物泛基因组学研究
Basic knowledge of pointer
电源自动测试系统NSAT-8000,精准高速可靠的电源测试设备
What is the safest app for stock account opening? Tell me what you know
【中国海洋大学】考研初试复试资料分享