当前位置:网站首页>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 name
I 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;
}
边栏推荐
- c语言:操作符详解
- Network layer protocol------IP protocol
- 使用map函数,对list中的每个元素进行操作 好像不用map
- MySQL的on duplicate key update 的使用
- LeetCode·23.合并K个升序链表·递归·迭代
- 你需要知道的ES6—ES13开发技巧
- 对一次生产环境产生OOM的记录,结论:除非在自己完全有把握的情况下,否则不要偷懒查询无用字段
- Why do so many people who teach themselves software testing give up later...
- How to make a deb package
- 在IDEA中使用JUnit4和JUnitGenerator V2.0自动生成测试模块
猜你喜欢
随机推荐
Swift RegexBuilder Vs. Raku Grammar
[The Beauty of Software Engineering - Column Notes] 31 | Is software testing responsible for product quality?
Why do so many people who teach themselves software testing give up later...
牛客网——业务分析-提取值
在IDEA中使用JUnit4和JUnitGenerator V2.0自动生成测试模块
5分钟搞懂MySQL - 行转列
mysql8安装步骤教程
MySQL 高级(进阶) SQL 语句 (一)
触摸屏状态机
JSESSIONID description in cookie
Image Restoration by Estimating Frequency Distribution of Local Patches
Motion Tuned Spatio-temporal Quality Assessmentof Natural Videos
MySQL的 DDL和DML和DQL的基本语法
转义字符笔记记录
数字货币期货现货交易技巧,把握关键进场的买入点!(纯干货)
Outsourcing worked for three years, it was abolished...
awk notes
DPW-SDNet: Dual Pixel-Wavelet Domain Deep CNNsfor Soft Decoding of JPEG-Compressed Images
Oblique document scanning and character recognition (opencv, coordinate transformation analysis)
用于命名实体识别的模块化交互网络