当前位置:网站首页>In depth C language (2) -- definition and use of structure
In depth C language (2) -- definition and use of structure
2022-07-29 03:33:00 【It is said that this is ZZY】
thorough C Language (2)—— Definition and use of structure
Column address : Address
List of articles
Statement of structure
Some explanation is needed when declaring the structure . All optional parts cannot be omitted —— There must be at least two of them .
The usual structure is as follows :
struct tag {
number-list
}variable-list;
What are the optional parts ?
tag,number-list,variable-list; There must be two of these three .
What are the functions of these three situations ? for instance :
struct {
int a;
char b;
float c;
}x,y[20],*z;
Here we use number-list,variable-list Two parts , To create a struct, And created three structures based on the above type Pattern The variable of , One is an array y The other is the variable x, The last one is a pointer , He points to this type of structure .
The above is called the first mode , But we don't use it very often , The second mode is commonly used :
//example 2
struct struct_type{
int a;
char b;
float c;
};
Only... Is used here tag,number-list Two parts , Different from the above , Here is the use of tag To declare a Pattern , Variables are not declared , We can define specific variables through this declaration , Implementation statement struct Mode is decoupled from specific variables .
How to use it is as follows :
struct struct_type W;
This is the same as the above veriable-list Make a statement x The variables are exactly the same .
The most common technique , The third model
The above two methods actually have some troubles , Especially when using , But it doesn't matter , We can also use typedef Keywords to simplify . here Struct_Type It actually becomes a variable , But this variable is a type variable ( actually C Without this Type variable , Here is convenient to understand ), That is to say, at this time Struct_Type It's a type of .
//example 3
typedef struct {
int a;
char b;
float c;
} Struct_Type;
The effect is as follows :
Struct_Type N;
Self reference of structure
If we want to create a pointer in the structure , This pointer can point to structural variables , That is, structure pointer , How to declare ?
Let's use the second mode to declare it. Try it :
//example 4
struct struct_type2{
int a;
struct struct_type2 *b;
};
This completes a structure that contains pointers to itself . Many complex data structures are actually implemented in this way , For example, the tree in the data structure column .
Of course, we often use the third technique . But there will be some small problems that need to be solved .
//example 5
typedef struct {
int a;
struct struct_type3 *b;
}struct_type3;
If we write like this, we will report an error , Because at this time struct_type3 Not yet defined . So how to solve the problem of nested declarations ?
The solution is , So declare :
//example 6
typedef struct Struct_Type2 *Struct_Pointer;
typedef struct Struct_Type2{
int a;
Struct_Pointer b;
}S;
Let's first give the tag a type declaration , Then use the tag to create a pointer to the structure . Of course you can too :
typedef struct Struct_Type2{
int a;
struct Struct_Type2 *b;
}S;
There is no problem at all , But you may wonder why you can declare the label like this ? Why can tags be used directly in structures by declaring them ?
Incomplete declaration
Sometimes , We must declare some interdependence , For example, the current structure pointer and structure are interdependent , This requires the use of incomplete declarations , He declares an identifier as a structure tag . Then you can use this label in There is no need to know the length of this structure in the declaration , Such as declaring the pointer of this structure . Next, the declaration of the structure will associate this tag with the member list .
The structure length here refers to the fact that the structure is not declared , I don't know what members are , So the size of a structure is uncertain .
//example 5
typedef struct Struct_Type2 *Struct_Pointer;
typedef struct Struct_Type2{
int a;
Struct_Pointer b;
}S;
This example uses incomplete declarations and typedef Keyword combination , First give Struct_Type2 Structure declares a tag ( Incomplete declaration ) Then declare a pointer type to the structure called Struct_Pointer.
Use of structure
The use of structure is a little more complicated than general variables , Because you need to access every member .
Direct access to structure members
The general form is to use symbols . To visit , Form the following :
Operators that access structure members have a high priority, which is almost the highest operator , If you don't know it yet, you can refer to the explanation of operator priority in the column .
Structural variable . member
for instance :
printf("%d",x.a=11);
The variables defined in the first formal example above are used here , Use... In this variable a Members to store data and output , The output is 11.
Indirect access to structure members
Using indirect access is certainly inseparable Indirect accessors and pointers , Above we explained how to define a structure pointer , Let's use it :
//example 6
typedef struct Struct_Type2 *Struct_Pointer;
typedef struct Struct_Type2{
int a;
Struct_Pointer b;
}S;
void test_struct_pointer(Struct_Pointer P)
{
(*P).a=660789;
printf("%d",(*P).a);
}
P The definition of uses the pointer definition method introduced above , Note the indirect accessors here * In fact, the priority is less than . So here we need to use () To enclose it , First, access the whole structure , Finally using . To access its members .
Elegant access to structure members
Of course, the above interview method is really too inelegant , We can use more elegant operators to solve this problem ,-> Operators are generally used like this :
Structure pointer -> member
This is very convenient , Compare the above way of using pointers , This is faster , Easy to write , More importantly, it supports more convenient nested access , Let's modify the above example again , Then look at the effect .
We first create a sub structure as the sub node of the previous structure , The effect is as follows :
typedef struct Struct_Type2 *Struct_Pointer;
typedef struct Struct_Type2{
int a;
Struct_Pointer b;
}S;
//example:
{
S test_struct_son;
test_struct.b = &test_struct_son;
test_struct_pointer2(&test_struct);
}
S It is the structure type defined by the method described at the end of the above .
Then assign it to the members of the structure through the direct accessor b in ,b It is actually a child node , The test function below is as follows :
//example 7
void test_struct_pointer2(Struct_Pointer P)
{
P->a=11;
P->b->a=12;
printf("%d\r\n",P->a);
printf("%d\r\n",P->b->a);
}
You can see the use of -> Nested access is a convenient choice .
边栏推荐
- Score addition and subtraction of force deduction and brushing questions (one question per day 7/27)
- 军品研制过程-转阶段
- Military product development process - transition phase
- Learn exkmp again (exkmp template)
- 04 | background login: login method based on account and password (Part 1)
- Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind
- AI_ Drug: VAE of molecular generation model (I)
- GJB common confused concepts
- How close can QA be to business code Direct exposure of defects through codediff
- How does DataGrid export and recover the entire database data, using a single SQL file
猜你喜欢

CUDA GDB prompt: /tmp/tmpxft**** cudafe1.stub. c: No such file or directory.

for_ Example of each usage

容斥原理

Idea configuration web container and war packaging

今晚7:30 | 连界、将门、百度、碧桂园创投四位大佬眼中的AI世界,是继续高深还是回归商业本质?...

Realize multi-level linkage through recursion

Ten thousand words detailed Google play online application standard package format AAB

Design of smoke temperature, humidity and formaldehyde monitoring based on single chip microcomputer

Whole process record of yolov3 target detection

最新二开版漫画小说听书三合一完整源码/整合免签接口/搭建教程/带采集接口
随机推荐
How to realize shortcut keys for interface scaling by vscade
STC单片机驱动1.8‘TFT SPI屏幕演示示例(含资料包)
Build redis environment under windows and Linux
Simple code implementation of decision tree
MySQL流程控制之while、repeat、loop循环实例分析
Configure vscade to realize ROS writing
Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing
(newcoder 15079)无关(容斥原理)
Numpy acceleration -- > cupy installation
How to deploy sentinel cluster of redis
How close can QA be to business code QA conducts testability transformation on business code
Rongyun real-time community solution
Suffix automata (SAM) board from Jly
Rdkit II: use rdkit screening to screen 2D pharmacophores of chemical small molecules
2 neural network toolbox NN
暴力递归到动态规划 01 (机器人移动)
Whole process record of yolov3 target detection
Rongyun IM & RTC capabilities on new sites
GJB common confused concepts
军品三大基线(功能基线、分配基线、产品基线)及基线包含的文件