一、结构体
结构体:是一种自定义的数据类型
可以把基本数据类型( int,bool, double …) 组合起来
例如:定义一个 Student 类型,
示例:
#include <stdio.h>#include <stdbool.h>#include <string.h>struct Student{int id;char name[32];char cellphone[32];bool sex;};int main(){struct Student s;s.id = 20180001;s.sex = true;strcpy_s(s.name, 32, "shaofa");strcpy_s(s.cellphone, 32, "13810012345");printf("学号: %d \n", s.id);printf("性别: %d \n", s.sex);printf("姓名: %s \n", s.name);printf("手机: %s \n", s.cellphone);getchar();return 0;}
结构体里可以添加多个字段,注意末尾须加分号
结构体变量
普通类型的变量:
int a = 12;double b = 33.4;
结构体类型的变量:
struct Student stu;
其中,stu为变量名,struct Student为变量类型
结构体变量的赋值
结构体变量的赋值:(分别给每个字段赋值)
struct Student s;s.id = 20180001;s.sex = true;strcpy_s(s.name, 32, "shaofa");strcpy_s(s.cellphone, 32, "13810012345");
注意:
用点号来表示字段,如s.id 表示变量s的id字段
struct xx{ }; 末尾要加分号
struct 定义写在函数前面 ( 先定义,后使用 )
struct Student 合在一起,视为一种新的类型来使用
struct Student s1;struct Student s2;
二、结构体数组
普通类型的数组:
int arr[30];
结构体的数组:
struct Student ss[30];
则第0号学生就是 ss[0] ,其学号就是ss[0].id
访问结构体数组:
struct Student ss[30];ss[0].id = 20180001;ss[0].sex = true;strcpy_s(ss[0].name, 32, "shaofa");strcpy_s(ss[0].cellphone, 32, "13810012345");
其中,ss[0] 就是第0号学生 ,其学号就是ss[0].id
示例:
#include <stdio.h>#include <stdbool.h>#include <string.h>struct Student{int id;char name[32];char cellphone[32];bool sex;};int main(){struct Student ss[30];ss[0].id = 20180001;ss[0].sex = true;strcpy_s(ss[0].name, 32, "shaofa");strcpy_s(ss[0].cellphone, 32, "13810012345");printf("学号: %d \n", ss[0].id);printf("性别: %d \n", ss[0].sex);printf("姓名: %s \n", ss[0].name);printf("手机: %s \n", ss[0].cellphone);getchar();return 0;}
三、结构体指针
普通类型的指针:
int a= 12;int* ptr = &a;*ptr = 13;
结构体的指针:
struct Student s;struct Student* ps = &s;//第一种访问方式:(*ps).id = 20180001;//第二种访问方式: ( 常用 )ps->id = 20180001;
使用 -> ,可以访问目标结构体的字段
struct Student s;struct Student* ps = &s;ps->id = 20180001;
其中,ps指向s
ps->id 就是 s.id ,即所指对象的id字段 ( 等价于 (*ps).id , 是一种简洁的写法)
结构体指针的应用
在作为参数时,一般使用结构体的指针
如:
void show(struct Student* stu){}
示例:
第一种写法
#include <stdio.h>#include <stdbool.h>#include <string.h>struct Student{int id;char name[32];char cellphone[32];bool sex;};int main(){struct Student s;struct Student* ps = &s;(*ps).id = 20180001;(*ps).sex = true;strcpy_s((*ps).name, 32, "shaofa");strcpy_s((*ps).cellphone, 32, "13810012345");getchar();return 0;}
第二种写法
#include <stdio.h>#include <stdbool.h>#include <string.h>struct Student{int id;char name[32];char cellphone[32];bool sex;};int main(){struct Student s;struct Student* ps = &s;ps->id = 20180001;ps->sex = true;strcpy_s(ps->name, 32, "shaofa");strcpy_s(ps->cellphone, 32, "13810012345");printf("学号: %d \n", ps->id);printf("性别: %d \n", ps->sex);printf("姓名: %s \n", ps->name);printf("手机: %s \n", ps->cellphone);getchar();return 0;
四、结构体嵌套
结构体嵌套
使用struct,可以创建自定义的类型
自定义的struct类型,和普通类型一样,可以作为别的struct的字段
示例1:
用 Point 表示坐标系中的一个点,每个点用(x,y)坐标
用 Line 表示坐标平面里的线, 起点start, 终点end
(1) 表示一个点
struct Point{double x;double y;};
(2) 表示一条线
struct Line{struct Point start;struct Point end;};
(3) 结构体的使用
struct Line l;// 起点 (0, 12)l.start.x = 0;l.start.y = 12;// 终点 (16, 0)l.end.x = 16;l.end.y = 0;
示例:
#include <stdio.h>#include <stdbool.h>#include <string.h>struct Point{double x;double y;};struct Line{struct Point start;struct Point end;};int main(){struct Line l;// 起点 (0, 12)l.start.x = 0;l.start.y = 12;// 终点 (16, 0)l.end.x = 16;l.end.y = 0;getchar();return 0;}
