当前位置:网站首页>深入C语言(2)——结构的定义与使用
深入C语言(2)——结构的定义与使用
2022-07-29 03:25:00 【据说这是zzy】
深入C语言(2)——结构的定义与使用
专栏地址:地址
结构的声明
声明结构的时候需要作出一些解释。所有可选的部分不能全部省略——他们至少要出现两个。
通常的结构如下:
struct tag {
number-list
}variable-list;
哪些是可选部分呢?
tag,number-list,variable-list;这三者中必定要出现两个。
这三种情况分别有什么作用呢?举个例子:
struct {
int a;
char b;
float c;
}x,y[20],*z;
这里就使用了number-list,variable-list两个部分,来创建了一个struct,并且创建了三个以上述类型结构为模式的变量,一个是数组y另一个是变量x,最后的是个指针,他指向这个类型的结构。
以上称为第一种模式,但我们不太常用,常用的是第二种模式:
//example 2
struct struct_type{
int a;
char b;
float c;
};
这里只使用了tag,number-list两个部分,与上文不同的是,这里是使用tag来声明一种模式,并不是声明了变量,我们可以通过这种声明再来定义具体的变量,实现声明struct模式与具体的变量进行解耦。
使用方法如下:
struct struct_type W;
这就和上文在veriable-list声明一个x变量一模一样。
最常用的技巧,第三种模式
上文两种方式实际上还有一些麻烦,尤其是在使用的时候,不过不要紧,我们还可以使用typedef关键字来简化。此时Struct_Type实际上成为了一个变量,只不过这个变量是一个类型变量(实际上C没有这个类型变量,这里是方便理解),也就是说此时Struct_Type是一种类型。
//example 3
typedef struct {
int a;
char b;
float c;
} Struct_Type;
使用效果如下:
Struct_Type N;
结构的自引用
如果我们想要在结构中创建一个指针,这个指针能指向结构变量,也就是结构指针,该如何声明呢?
我们使用第二种模式来声明一下试一试:
//example 4
struct struct_type2{
int a;
struct struct_type2 *b;
};
这样就完成了一个包含能指向自身的指针的结构。很多复杂的数据结构实际上还是通过这样的方式来实现的,比如数据结构专栏里面要讲的树。
当然我们常用的还是第三种技巧。但是会有些小问题需要解决。
//example 5
typedef struct {
int a;
struct struct_type3 *b;
}struct_type3;
我们这样写就会报错,因为此时struct_type3还没有被定义出来。那么怎么样解决嵌套声明问题呢?
解决办法就是,这样声明:
//example 6
typedef struct Struct_Type2 *Struct_Pointer;
typedef struct Struct_Type2{
int a;
Struct_Pointer b;
}S;
我们先给标签一个类型声明,然后使用标签给结构体来创建一个指针。当然你也可以这样:
typedef struct Struct_Type2{
int a;
struct Struct_Type2 *b;
}S;
这样也是完全没有问题的,但是你会有疑问为什么能这样先声明标签呢?为什么通过声明标签就可以直接在结构中使用呢?
不完全声明
有些时候,我们必须声明一些相互存在的依赖,比如当前的结构指针和结构就互相构成了依赖,这就要使用到不完全声明了,他声明一个作为结构标签的标识符。然后可以把这个标签用在不需要知道这个结构的长度的声明中,如声明这个结构的指针。接下来结构的声明把这个标签和成员列表会联系在一起。
这里的指的结构长度就是因为结构没声明,不知道成员都有哪些都是哪些,所以一个结构的大小是不确定的。
//example 5
typedef struct Struct_Type2 *Struct_Pointer;
typedef struct Struct_Type2{
int a;
Struct_Pointer b;
}S;
这个例子就是使用不完全声明和typedef关键字组合,先给Struct_Type2结构声明了一个标签(不完全声明)然后再给结构声明一个指针类型叫 Struct_Pointer。
结构的使用
结构的使用相较于一般的变量来来说稍微复杂一点,因为要针对每一个成员进行访问。
结构成员的直接访问
一般的形式就是使用符号.来访问,形式如下:
访问结构成员的操作符优先级很高几乎就是最高的运算符,如果你还不知道可以参考一下专栏关于运算符优先级的讲解。
结构体变量.成员
举个例子:
printf("%d",x.a=11);
这里使用了上文第一种形式例子中定义的的变量,使用这个变量中的a成员来存储数据并输出,输出结果是11.
结构成员的间接访问
使用间接访问当然离不开间接访问符和指针,上文我们说明了如何定义一个结构指针,我们就来使用一下:
//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的定义使用的是上文介绍的指针定义方法,需要注意的是我们这里的间接访问符*实际上是优先级小于.的所以这里要先使用()来将其括起来,首先访问到整个结构体,最后使用.来访问其成员。
结构成员的优雅访问
当然上文的访问方式实在是太不优雅了,我们可以使用更优雅的运算符来解决这个问题,->运算符一般这样使用:
结构体指针->成员
这就是很方便了,对比上文使用指针的方式,这种就更加快捷,书写简便,跟重要的是它支持更方便的嵌套访问,我们再来修改上边的例子,然后再看看效果。
我们首先创建一个子结构作为上一个结构的子节点,效果如下:
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就是上文最后介绍的方法定义的结构类型。
然后通过直接访问符将其赋值给结构的成员b中,b实际上是一个子节点,下边的测试函数如下:
//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);
}
可以看到使用->来进行嵌套访问是一个很方便地选择。
边栏推荐
- [freeswitch development practice] media bug obtains call voice flow
- 力扣刷题之数组序号计算(每日一题7/28)
- 基于单片机烟雾温湿度甲醛监测设计
- 力扣刷题之分数加减运算(每日一题7/27)
- "PHP Basics" output approximate value of PI
- C language programming | exchange binary odd and even bits (macro Implementation)
- MYCAT read / write separation configuration
- C traps and defects Chapter 2 syntax "traps" 2.6 problems caused by "hanging" else
- 美联储再加息,75基点 鲍威尔“放鸽”,美股狂欢
- makefile详解
猜你喜欢

mysql的timestamp存在的时区问题怎么解决

How does DataGrid export and recover the entire database data, using a single SQL file

Arm architecture and neural network

Simple code implementation of decision tree

makefile详解

机器学习【Numpy】

【科技1】

A case of gradually analyzing the splitting of classes -- colorful ball collisions

Summary of basic knowledge points of C language

04 | background login: login method based on account and password (Part 1)
随机推荐
Suffix automata (SAM) board from Jly
Anaconda offline installation environment
Complexity analysis learning
带你来浅聊一下,单商户功能模块汇总
基于单片机烟雾温湿度甲醛监测设计
Unity game special effects
力扣刷题之分数加减运算(每日一题7/27)
C traps and defects Chapter 3 semantic "traps" 3.8 operators &, |, and!
Regular expression bypasses WAF
1.4 nn. Module neural network (II)
Simple code implementation of K-means clustering
Watermelon book learning Chapter 6 -- SVM
暴力递归到动态规划 01 (机器人移动)
Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it
Several methods of converting object to string
Shardingsphere's level table practice (III)
13_ UE4 advanced_ Montage animation realizes attack while walking
Shortcut key for adjusting terminal size in ROS
数字孪生实际应用案例-智慧能源篇
Example analysis of while, repeat and loop loops in MySQL process control