当前位置:网站首页>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 to add elements to the header, or tail of an array

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

Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment

15 -- k points closest to the origin

15 -- 最接近原点的 K 个点

basic_ String mind map

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

‘make_ unique’ is not a member of ‘std’

Review of arrays and pointers triggered by a topic

Clinical chemistry | zhangjianzhong / Xu Jian develop single cell precision diagnosis and treatment technology for Helicobacter pylori
随机推荐
QQ love talk candy love talk content acquisition and storage
QT inline dialog
New title of PTA
C language LNK2019 unresolved external symbols_ Main error
[deep learning] multi task learning of multiple datasets data sets missing labels
[Ocean University of China] information sharing for the first and second examinations of postgraduate entrance examination
From 408 to independent proposition, 211 to postgraduate entrance examination of Guizhou University
移除区间(贪心)
Using Sphinx to automatically generate API documents from py source files
Review of arrays and pointers triggered by a topic
Open a restaurant
Time stamp calculation and audio-visual synchronization of TS stream combined video by ffmpeg protocol concat
Mutationobserver listens for DOM changes
Clinical Chemistry | 张建中/徐健开发幽门螺杆菌单细胞精准诊疗技术
Ideal L9 in the eyes of the post-90s: the simplest product philosophy, creating the most popular products
JS get the height and width corresponding to the box model (window.getcomputedstyle, dom.getboundingclientrect)
[try to hack] vulhub shooting range construction
Is it safe to open an online stock account? Who knows
Qmake uses toplevel or topbuilddir
Kubernetes understands kubectl/ debugging