当前位置:网站首页>The structure of knowledge in the corners of the C language
The structure of knowledge in the corners of the C language
2022-07-30 21:06:00 【Gou Bar Listening Song_0】
目录
Direct component selection 双目操作符 " . "
Indirect component selection 双目操作符 " -> "
The structure is initialized uniformly
结构体
基本语法
Define a new structure
A structure is a data type defined by our programmers
struct 结构体名
{
类型1 成员变量名1
类型2 成员变量名2
...
};实例化结构体
struct 结构体名 a; //ais the new typestruct An instance of the struct nameI don't know if everyone is familiar with the instantiation of the structure ,其实“struct 结构体名”与“int”是一个意思,只不过“struct 结构体名”is our own type,而“int”是CIt is already defined in the language library,使用方法都一样,就如同我们使用“int”一样,定义一个4字节32bit的整形数,And we use our own defined structure is,定义一个“struct 结构体名”的变量.
访问成员变量
如上述,After we instantiate the struct,You can access member variables through some operators.For example, how should the member variables in the following structure be accessed
struct date
{
int y;
int m;
int d;
};
struct student
{
char name[32];
int num;
float score;
struct date birthday;
};Direct component selection 双目操作符 " . "
普通访问
组合类型变量.成员变量 ==》To get the member variable inside the composite type variable
Let's now access the member variables of the above structure after instantiation
struct student s;
s.name;
s.num;
s.score;
s.birthday;
s.birthday.y;
s.birthday.m;
s.birthday.d;怎么样,This is still very simple,其中要注意的是,in an externally accessed struct,There are also structures,We need to first access the struct in the current struct,Then access the specific member variables.as we visit struct date 中的 y ,我们先访问struct student中的struct date birthday,再访问 struct date 中的 y,代码实现就是 s.birthday.y.
成功访问后,We can perform assignments and other operations on member variables.
指针访问
(*结构体指针).成员变量
*结构体指针 ==》The structure variable pointed to
结构体变量.成员变量
有了上面的例子,这个就很简单啦
struct student *p = malloc(sizeof(struct student));
(*p).name;
(*p).num;
(*p).score;
(*p).birthday;
(*p).birthday.y;
(*p).birthday.m;
(*p).birthday.d;Indirect component selection 双目操作符 " -> "
用法:结构体指针->成员变量名
It is still the structure we defined above,我们要如何访问呢
struct student *p = malloc(sizeof(struct student));
p->name;
p->num;
p->score;
p->birthday;
p->birthday.y;
p->birthday.m;
p->birthday.d;值得注意的是,In this case again,访问birthdaymember variables in ,因为没有对date进行操作,Still use direct component selection,So coding is implemented as:p->birthday.m;.
综上,There are three ways to access member variables of a structure,According to this example,则是
struct student s;
s.成员变量;
struct student *p = malloc(sizeof(struct student));
(*p).成员变量;
struct student *p = malloc(sizeof(struct student));
p->成员变量;结构体初始化
The structure is initialized uniformly
It is how the instances defined later have an initial value,Let's stick to the example above
struct date
{
int y = 2000;
int m = 1;
int d = 1;
};
struct student
{
char name[32] = 张三;
int num = 20000101;
float score = 0;
struct date birthday;
};在定义结构体时,You can assign values to the member variables in the structure.This way each instantiated object will have these initial values after that.
直接初始化
If we are not creating the structure,就初始化,Or like changing the initialized value,We can directly initialize the structure instance assignment.
in the order in which they were defined,依次初始化各成员变量,用逗号分开:
struct student s ={
"张三",
20000101,
0,
{
2000,1,1
},
}当然,We can also define without order,Then we need to add the member variable name:
struct student s = {
.num = 20000101,
.name = "张三",
.birthday = {
2000,1,1
},
.score = 0
}结构体数组的初始化
按顺序初始化
struct student class[3] =
{
//class[0]
{
.name = "张三",
.num = 20000101,
.score = 0,
.birthday = {2001,01,01}
},
//class[1]
{
.name = "李四",
.num = 20000102,
.score = 0,
.birthday = {2001,01,02}
},
//class[2]
{
.name = "王五",
.num = 20000103,
.score = 0,
.birthday = {2001,01,03}
}
};同样的,We can also initialize out of order ,Medical array subscript to achieve,不过这种方式,不同编译器下,Circumstances or restrictions are different
struct student class[3] =
{
[1] = {
.name = "张三",
.num = 20000101,
.score = 0,
.birthday = {2001,01,01}
},
[0] = {
.name = "李四",
.num = 20000102,
.score = 0,
.birthday = {2001,01,02}
},
[2] = {
.name = "王五",
.num = 20000103,
.score = 0,
.birthday = {2001,01,03}
}
};实例操作
有了以上的知识储备,Let's practice our hands
First define a structure to describe the situation of a student(姓名、学号、成绩,出生年、月、日),Then define an array of structures to describe the students in our class(3个),And enter each student's information from the keyboard,and output to the terminal.
代码实现
#include<stdio.h>
struct date
{
int y;
int m;
int d;
};
struct student
{
char name[32];
int num;
float score;
struct date birthday;
};
int main()
{
struct student CS[3];
for(int i = 0;i < sizeof(CS) / sizeof(struct student);i++)
{
scanf("%d%d%d%s%d%f",&CS[i].birthday.y,&CS[i].birthday.m,&CS[i].birthday.d,CS[i].name,&CS[i].num,&CS[i].score);
}
for(int i = 0;i < sizeof(CS) / sizeof(struct student);i++)
{
printf("%d %d\t%d\t%s\t%d\t%f\n",CS[i].birthday.y,CS[i].birthday.m,CS[i].birthday.d,CS[i].name,CS[i].num,CS[i].score);
}
return 0;
}边栏推荐
猜你喜欢
随机推荐
用于命名实体识别的模块化交互网络
MySQL的Replace用法详解
Typescript 严格模式有多严格?
【回归预测-CNN预测】基于卷积神经网络CNN实现数据回归预测附matlab代码
IDEA2018.3.5 cancel double-click Shift shortcut
Mysql8创建用户以及赋权操作
Image Restoration by Estimating Frequency Distribution of Local Patches
kubernetes
想要写出好的测试用例,先要学会测试设计
2022-07-29 mysql/stonedb慢SQL-Q17-分析
Deep Non-Local Kalman Network for VideoCompression Artifact Reduction
Network layer protocol------IP protocol
GPGGA NTRIP RTCM 笔记
mysql死锁
新书上市 |《谁在掷骰子?》在“不确定性时代”中确定前行
GPGGA NTRIP RTCM Notes
外包干了三年,废了...
Motion Tuned Spatio-temporal Quality Assessmentof Natural Videos
6.3有定型性 第七章
js堆和栈









