结构体
1 - 结构体是一种自定义的数据类型,它是由其它几种数据类型构成的
2 - 结构体内存大小:以最大成员变量类型所占的空间为分配单位,按结构体成员声明的顺序自上而下分配。注:结构体所占空间大小理论上应是所有成员所占内存之和,但实际上是最大成员变量所占空间的最小整数倍
3 - 代码示例:使用结构体
1 #include <stdio.h>
2
3 // 方式二:可使用宏义:用一个符号常量来表示一个结构类型
4 #define PER struct person
5 PER{
6
7 char name[20];
8 char sex;
9 int age;
10 float score;
11 };
12 PER gentleman,madam;
13
14
15 // 方式一:常规定义
16 struct stu
17 {
18 char name[20];
19 char sex;
20 int age;
21 float score;
22 };
23 struct stu boy,girl={"Rose",'f',22,99.9}; // 声明了两个结构变量,并把其中一个变量初始化
24
25
26 // 方式三:定义结构类型时同时声明结构变量
27 struct animal
28 {
29 char name[20];
30 char sex;
31 int age;
32 }cat,dog;
33
34
35 // 直接定义结构变量
36 struct
37 {
38 char color[20];
39 float price;
40 }bike,car={"RED",999999.8};
41
42 int main(int argc, const char * argv[]) {
43
44 struct stu boy = {"Jack",'m',21,99.8};
45 printf("姓名:%s,性别:%c \n",boy.name,boy.sex); // 姓名:Jack,性别:m
46 printf("姓名:%s,性别:%c \n",girl.name,girl.sex); // 姓名:Rose,性别:f
47 printf("颜色:%s,价格:%f \n",car.color,car.price);// 颜色:RED,价格:999999.812500
48 bike = car;// 相同的结构体之间可以直接赋值
49 bike.price = 8888.99;
50 printf("颜色:%s,价格:%f \n",bike.color,bike.price); // 颜色:RED,价格:8888.990234
51
52 return 0;
53 }
typedef
1 - 它为现有类型创建一个新的名字,可用来编写更美观和可读的代码。功能就是将原有的名字替换掉,用新的名字来代替,且不改变其代表的含义。它的声明有助于创建和平台无关类型,甚至能隐藏复杂、难以理解的语法
2 - typedef 在结构体中的使用
1 #include <stdio.h>
2
3 typedef int intDemo;
4
5 // 方式一
6 struct studentA{
7
8 int number ;
9 char names [20];
10 char sex [20];
11 int age;
12 float score;
13 };
14 typedef struct studentA StuA;
15
16 // 方式二
17 typedef struct dateInfo{
18
19 intDemo mouth;// 使用重定义的标志符代替 int
20 int day;
21 int year;
22 }Date;// Date 是结构体名称,并非结构体变量
23
24 // 结构体嵌套
25 typedef struct studentB{
26
27 char names[20];
28 Date birthday;
29 }StuB;
30
31
32 int main(int argc, const char * argv[]) {
33
34 // 赋值:顺序要和定义时的顺序保持一致
35 struct studentA student1 = {110110,"小红","女"};
36 printf("学号:%d 姓名:%s 性别:%s 年龄:%d\n",student1.number,student1.names, student1.sex,student1.age);
37
38 //student1.names = "花花";// 不能直接对数组进行赋值运算,编译报错
39 strcpy(student1.names,"小白");// 使用 strcpy 函数,进行修改
40 printf("修改后的姓名:%s,\n",student1.names);
41
42 StuB boy ={"小明",{1,11,1990}};
43 printf("%s %d-%d-%d\n",boy.names,boy.birthday.year,boy.birthday.mouth,boy.birthday.day);
44
45 return 0;
46 }
3 - 找出年龄最小的和评分最高的
1 stA student1={1,"Linda Lusardi","female",59,98.5};
2 stA student2={2,"Helen Flanagan","female",27,90.5};
3 stA student3={3,"Keeley Hazell","female",32,99.5};
4
5 stA max ={0};
6 max = student1.score > student2.score ? student1 : student2;
7 max = max.score > student3.score ? max : student3;
8 printf("三位女神用户体验评分最高的是%s:%.2f\n",max.names,max.score);
9
10 stA min ={0};
11 min = student1.age < student2.age ? student1 : student2;
12 min = min.age < student3.age ? min : student3;
13 printf("三位女神芳龄最小的是%s:%d\n",min.names,min.age);
结构体数组
1 - 代码示例
1 #include <stdio.h>
2 typedef struct studentA{
3
4 int number;
5 char names [20];
6 char sex [20];
7 int age;
8 float score;
9 }stA;
10
11 typedef struct dateInfo{
12
13 int year;
14 int mouth;
15 int day;
16 }Date;
17
18 int main(int argc, const char * argv[]) {
19 // 结构体数组初始化
20 stA general[5]={{1111,"米尼兹","man",24,98.5},{2222,"高野五十六","female",25,90.5},{3333,"巴顿","man",21,94.5},{4444,"戴高乐","female",23,99.5},{5555,"隆美尔","female",20,97}};
21 // 访问结构体数组中某一具体成员变量
22 printf("%s\n",general[0].names);
23
24 stA max ={0};
25 for (int i=0; i<5; i++) {
26 if (max.score<general[i].score) {
27 max=general[i];
28 }
29 }
30 printf("\n顶级战将相关信息:编号:%d 姓名:%s 性别:%s 年龄:%d 战绩评分%.2f\n",max.number,max.names,max.sex,max.age,max.score);
31
32
33 // 按战绩进行排名(冒泡排序)
34 for (int i=0; i<5-1; i++) {
35 for (int j=0; j<5-1-i; j++) {
36 if (general[j].score<general[j+1].score) {
37 max = general[j];
38 general[j]=general[j+1];
39 general[j+1]=max;
40 }
41 }
42 }
43 printf("\n战绩评分排名如下:");
44 for (int i=0; i<5; i++) {
45 printf("\n姓名:%s\t-----战绩评分:%.2f",general[i].names,general[i].score);
46 }
47 printf("\n");
48
49 return 0;
50 }
日志信息