8种机械键盘轴体对比
本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?
一.说明
此篇文章关于C语言中结构体和枚举的用法和总结
共包括知识点[2019-08-09]
1.为什么需要结构体
2.如何定义一个结构体
3.怎样使用结构体变量
4.如何取出结构体变量中的每一成员
5.结构体变量的运算
6.结构体变量和结构体变量指针作为函数参数传递的问题
7.枚举
8.枚举的应用
二.为什么需要结构体
为了表示一些复杂的事物,而普通的基本类型,无法满足实际要求
什么叫结构体
把一些基本类型数据组合在一起形成的一个新的数据类型,这个叫做结构体1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# include
struct Student
{
int age;
float score;
char sex;
};
int main(void)
{
struct Student st = {80, 66.6, 'F'};
return 0;
}
三.如何定义一个结构体1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22三种方式,推荐使用第一种
//第一种方式 【常用】
struct Student
{
int age;
float score;
char sex;
};
//第二种方式
struct Student2
{
int age;
float score;
char sex;
} st2;
//第三种方式
struct
{
int age;
float score;
char sex;
} st3;
四.怎样使用结构体变量
赋值和初始化
定义的同时可以整体赋初值
如果定义完之后,则只能单个的赋初值1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24// 结构体变量的赋值和初始化
# include
struct Student
{
int age;
float score;
char sex;
};
int main(void)
{
struct Student st = {80, 66.6, 'f'}; //初始化 定义的同时赋初值
struct Student st2;
st2.age = 10;
st2.score = 88;
st2.sex = 'F';
printf("%d %f %cn", st.age, st.score, st.sex);
printf("%d %f %cn", st2.age, st2.score, st2.sex);
return 0;
}
五.如何取出结构体变量中的每一个成员【重点】结构体变量名.成员名
指针变量名 -> 成员名
指针变量名 -> 成员名 在计算机内部会被转化成 (*指针变量名).成员名的方式来执行
所以说这两种方式是等价的1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28//如何取出结构体变量中的每一个成员【重点】
# include
struct Student
{
int age;
float score;
char sex;
};
int main(void)
{
struct Student st = {80, 66.6, 'F'};
struct Student * pst = &st; //&st不能改成st
st.score = 10; //第一种方式
pst->age = 88; //第二种方式,经常会用这种方式
printf("%d %fn", st.age, pst->score);
return 0;
}
/*
1.pst -> age 在计算机内部会被转换成 (*pst).age, 这是一种硬性规定
2.所以pst -> age 等价于 (*pst.age) 等价于 st.age
3.我们之所以知道pst -> age 等价于 st.age , 是因为pst->age是被转化成了(*pst).age
4.pst->age的含义:pst所指向的那个结构体变量中的age这个成员
*/
六.结构体变量的运算
结构体变量不能相加 、相见、也不能相互乘除
但结构体变量可以相互赋值
例子:
struct Student
{
int age;
char sex;
char name[100];
};
struct Stduent st1, st2;
//st1 + st2 st1*st2 st1/st2 //都是错误的
//st1 = st2 或者是 st2 = st1都是正确的
七.结构体变量和结构体变量指针作为函数参数传递的问题
推荐使用结构体指针变量作为函数参数来传递
指针的优点之一:
快速的快递数据;
耗用内存小
执行速度块1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61// 通过函数完成对结构体变量的输入和输出
# include
# include
struct Student
{
int age;
char sex;
char name[100];
};
void InputStudent(struct Student *);
void OutputStudent(struct Student *);
int main(void)
{
struct Student st;
printf("st所占用的字节空间:%dn", sizeof(st));
InputStudent(&st); //对结构体变量输入 必须发送st的地址,
OutputStudent(&st); //对结构体的输出,可以发送st的地址,也可以直接发送st的内容,但是为了减少内存的耗费,也为了提高执行的速度,推荐地址
return 0;
}
void InputStudent(struct Student * pStu) //pStu只占4个字节
{
(*pStu).age = 10;
strcpy(pStu->name, "张三");
pStu -> sex = 'F';
}
void OutputStudent( struct Student * ss)
{
printf("%d %c %sn", ss->age, ss->sex, ss->name);
}
/*
//本函数无法修改主函数中st的值, 所以本函数是错误的
void InputStudent(struct Student stu)
{
stu.age = 10;
strcpy(stu.name, "张三"); //不能写成stu.name = "张三";
stu.sex = 'F';
}
*/
/*
在VC++6.0下输出的结果为:
-------------------------
st所占用的字节空间:108
10 F 张三
Press any key to continue
-------------------------
总结:
当函数需要对结构体的变量进行改变式,需要发送结构体变量的地址
当函数不需要发送结构体的变量时或者只是对结构体变量的输出,可以发送结构体变量的地址,也可以发送结构体变量的内容
但是为了减少内存的耗费,也为了提高执行的速度,推荐地址
依据上面的例子来讲,
发送内容的话,形参那里会请求108个字节的空间去储存实参的内容,
发送地址的话,只需要4个字节的空间去储存实参的地址,不仅占用内存小,而且执行速度更快
*/
八. 枚举
1. 什么是枚举
把一个事物所有可能的取值一一列举出来
2. 怎样使用枚举1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24# include
//只定义了一个数据类型,并没有定义变量,该数据类型的名字是 enum WeekDay
enum WeekDay
{
MonDay, TuesDay, WednesDay, ThursDay, FriDay, SaturDay, SunDay
};
int main(void)
{
// int day; //day定义成int类型不合适
enum WeekDay day = Wednesday;
printf("%dn", day);
return 0;
}
/*
在VC++6.0下输出的结果为:
----------------------------
2
Press any key to continue
----------------------------
*/
3. 枚举的优缺点
1.使代码安全
2.书写麻烦
九.枚举的简单应用1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54# include
enum WeekDay
{
MonDay, TuesDay, WednesDay, ThursDay, FriDay, SatursDay, SunDay
};
void f(enum WeekDay i)
{
switch (i)
{
case 0;
printf("MonDayn");
break;
case 1;
printf("TuesDayn");
break;
case 2;
printf("WednesDayn");
break;
case 3;
printf("ThurDayn");
break;
case 4;
printf("FriDayn");
break;
case 5;
printf("SaturDayn");
break;
case 6;
printf("SunDayn");
break;
}
}
int main(void)
{
f(FriDay);
return 0;
}
/*
在VC++6.0下输出的结果为:
--------------------------
FriDay
Press any key to continue
--------------------------
总结:
枚举的优缺点:
1. 使代码安全
2. 书写麻烦
*/