当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

JS get the height and width corresponding to the box model (window.getcomputedstyle, dom.getboundingclientrect)

Heavyweight! The domestic IDE is released and developed by Alibaba. It is completely open source! (high performance + high customization)

Gif动画怎么在线制作?快试试这款gif在线制作工具

Source code analysis of synergetics and ntyco

Js- get the mouse coordinates and follow them

Review of arrays and pointers triggered by a topic

SPARQL learning notes of query, an rrdf query language

JGG | overview of duhuilong group of Hebei University Research on plant pan genomics

What moment makes you think there is a bug in the world?

有哪个瞬间让你觉得这个世界出bug了?
随机推荐
JS Base64 Library Learning
High precision addition
Compile Caffe's project using cmake
网上股票开户安不安全?有谁知道呢
About the problem of kicad stuck in win10 version, version 6 x
JS to add elements to the header, or tail of an array
Basic usage of markdown (plain text and grammar)
多张动图怎样合成一张gif?仅需三步快速生成gif动画图片
分饼干问题
Review of arrays and pointers triggered by a topic
How to combine multiple motion graphs into a GIF? Generate GIF animation pictures in three steps
Qt: Pro project file
What moment makes you think there is a bug in the world?
Go closure usage example
关于win10 版本kicad 卡死的问题, 版本6.x
New good friend Pinia, leading the new era of state management
现在股票开户用什么app最安全?知道的给说一下吧
Function of getinstance() method
JS to verify whether the string is a regular expression
Yolov3 spp Darknet version to caffemodel and then to OM model