一、结构体

结构体:是一种自定义的数据类型
可以把基本数据类型( int,bool, double …) 组合起来

例如:定义一个 Student 类型,
示例:

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. struct Student
  5. {
  6. int id;
  7. char name[32];
  8. char cellphone[32];
  9. bool sex;
  10. };
  11. int main()
  12. {
  13. struct Student s;
  14. s.id = 20180001;
  15. s.sex = true;
  16. strcpy_s(s.name, 32, "shaofa");
  17. strcpy_s(s.cellphone, 32, "13810012345");
  18. printf("学号: %d \n", s.id);
  19. printf("性别: %d \n", s.sex);
  20. printf("姓名: %s \n", s.name);
  21. printf("手机: %s \n", s.cellphone);
  22. getchar();
  23. return 0;
  24. }

结构体里可以添加多个字段,注意末尾须加分号

结构体变量
普通类型的变量:

  1. int a = 12;
  2. double b = 33.4;

结构体类型的变量:

  1. struct Student stu;

其中,stu为变量名,struct Student为变量类型

结构体变量的赋值
结构体变量的赋值:(分别给每个字段赋值)

  1. struct Student s;
  2. s.id = 20180001;
  3. s.sex = true;
  4. strcpy_s(s.name, 32, "shaofa");
  5. strcpy_s(s.cellphone, 32, "13810012345");

注意:
用点号来表示字段,如s.id 表示变量s的id字段
struct xx{ }; 末尾要加分号
struct 定义写在函数前面 ( 先定义,后使用 )
struct Student 合在一起,视为一种新的类型来使用

  1. struct Student s1;
  2. struct Student s2;

二、结构体数组

普通类型的数组:

  1. int arr[30];

结构体的数组:

  1. struct Student ss[30];

则第0号学生就是 ss[0] ,其学号就是ss[0].id
访问结构体数组:

  1. struct Student ss[30];
  2. ss[0].id = 20180001;
  3. ss[0].sex = true;
  4. strcpy_s(ss[0].name, 32, "shaofa");
  5. strcpy_s(ss[0].cellphone, 32, "13810012345");

其中,ss[0] 就是第0号学生 ,其学号就是ss[0].id
示例:

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. struct Student
  5. {
  6. int id;
  7. char name[32];
  8. char cellphone[32];
  9. bool sex;
  10. };
  11. int main()
  12. {
  13. struct Student ss[30];
  14. ss[0].id = 20180001;
  15. ss[0].sex = true;
  16. strcpy_s(ss[0].name, 32, "shaofa");
  17. strcpy_s(ss[0].cellphone, 32, "13810012345");
  18. printf("学号: %d \n", ss[0].id);
  19. printf("性别: %d \n", ss[0].sex);
  20. printf("姓名: %s \n", ss[0].name);
  21. printf("手机: %s \n", ss[0].cellphone);
  22. getchar();
  23. return 0;
  24. }

三、结构体指针

普通类型的指针:

  1. int a= 12;
  2. int* ptr = &a;
  3. *ptr = 13;

结构体的指针:

  1. struct Student s;
  2. struct Student* ps = &s;
  3. //第一种访问方式:
  4. (*ps).id = 20180001;
  5. //第二种访问方式: ( 常用 )
  6. ps->id = 20180001;

使用 -> ,可以访问目标结构体的字段

  1. struct Student s;
  2. struct Student* ps = &s;
  3. ps->id = 20180001;

其中,ps指向s
ps->id 就是 s.id ,即所指对象的id字段 ( 等价于 (*ps).id , 是一种简洁的写法)
结构体指针的应用
在作为参数时,一般使用结构体的指针
如:

  1. void show(struct Student* stu)
  2. {
  3. }

示例:
第一种写法

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. struct Student
  5. {
  6. int id;
  7. char name[32];
  8. char cellphone[32];
  9. bool sex;
  10. };
  11. int main()
  12. {
  13. struct Student s;
  14. struct Student* ps = &s;
  15. (*ps).id = 20180001;
  16. (*ps).sex = true;
  17. strcpy_s((*ps).name, 32, "shaofa");
  18. strcpy_s((*ps).cellphone, 32, "13810012345");
  19. getchar();
  20. return 0;
  21. }

第二种写法

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. struct Student
  5. {
  6. int id;
  7. char name[32];
  8. char cellphone[32];
  9. bool sex;
  10. };
  11. int main()
  12. {
  13. struct Student s;
  14. struct Student* ps = &s;
  15. ps->id = 20180001;
  16. ps->sex = true;
  17. strcpy_s(ps->name, 32, "shaofa");
  18. strcpy_s(ps->cellphone, 32, "13810012345");
  19. printf("学号: %d \n", ps->id);
  20. printf("性别: %d \n", ps->sex);
  21. printf("姓名: %s \n", ps->name);
  22. printf("手机: %s \n", ps->cellphone);
  23. getchar();
  24. return 0;

四、结构体嵌套

结构体嵌套
使用struct,可以创建自定义的类型
自定义的struct类型,和普通类型一样,可以作为别的struct的字段
示例1:
用 Point 表示坐标系中的一个点,每个点用(x,y)坐标
用 Line 表示坐标平面里的线, 起点start, 终点end
(1) 表示一个点

  1. struct Point
  2. {
  3. double x;
  4. double y;
  5. };

(2) 表示一条线

  1. struct Line
  2. {
  3. struct Point start;
  4. struct Point end;
  5. };

(3) 结构体的使用

  1. struct Line l;
  2. // 起点 (0, 12)
  3. l.start.x = 0;
  4. l.start.y = 12;
  5. // 终点 (16, 0)
  6. l.end.x = 16;
  7. l.end.y = 0;

示例:

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. struct Point
  5. {
  6. double x;
  7. double y;
  8. };
  9. struct Line
  10. {
  11. struct Point start;
  12. struct Point end;
  13. };
  14. int main()
  15. {
  16. struct Line l;
  17. // 起点 (0, 12)
  18. l.start.x = 0;
  19. l.start.y = 12;
  20. // 终点 (16, 0)
  21. l.end.x = 16;
  22. l.end.y = 0;
  23. getchar();
  24. return 0;
  25. }