什么是结构体
结构体是一种自定义的复合型的数据类型,可以由多种不同数据类型组成的数据集合.
结构体的声明
struct 结构体名
{
成员列表
};
struct Student
{
int age;
float height;
char sex;
double weight;
char name[20];
};
- 声明了一个Student类型的结构体,其包含了5种不同数据类型的成员.
结构体变量的声明
- 直接在结构体声明的末尾添加一个变量名,不过这个结构体变量是全局的
struct Student
{
int age;
float height;
char sex;
float weight;
char name[20];
}student;
声明了一个结构体变量student,是一个全局的结构体变量.
- 直接在结构体类型后面 加上变量名
struct 结构体名 变量名
struct Student student1;
struct Student student2;
声明了两个结构体变量student1与student2,属于局部的结构体变量.
结构体变量的初始化
- 在声明结构体变量的时候直接初始化.
struct Student student1 = {30,172.5,'1',150.55,"liyanyan"};
大括号中的数据与结构体中的变量一一对应.
- 先声明结构体变量然后再进行初始化.
struct Student student2;
student2.age = 30;
student2.height = 172.5;
student2.sex = '1';
student2.weight = 150.55;
strcpy(student2.name,"liyanyan");
结构体变量访问成员
结构体变量名.成员名
printf(" age = %d\n",student2.age);
printf(" height = %f\n",student2.height);
printf(" sex = %c\n",student2.sex);
printf(" weight = %f\n",student2.weight);
printf(" name = %s\n",student2.name);
Snip20201217_26.png
通过(.)点号直接访问成员.
结构体的嵌套
struct BirthDay
{
int year;
int month;
int day;
};
struct Student
{
int age;
float height;
char sex;
double weight;
char name[20];
struct BirthDay birthday; //生日日期结构体
};
Student结构体中嵌套了BirthDay结构体
struct Student student1 = {30,172.5,'1',150.55,"liyanyan",{1990,07,07}};
struct Student student2;
student2.age = 30;
student2.height = 172.5;
student2.sex = '1';
student2.weight = 150.55;
strcpy(student2.name,"liyanyan");
student2.birthday.year = 1990;
student2.birthday.month = 07;
student2.birthday.day = 07;
printf(" age = %d\n",student2.age);
printf(" height = %f\n",student2.height);
printf(" sex = %c\n",student2.sex);
printf(" weight = %f\n",student2.weight);
printf(" name = %s\n",student2.name);
printf(" birthday.year = %d\n",student2.birthday.year);
printf(" birthday.month = %d\n",student2.birthday.month);
printf(" birthday.day = %d\n",student2.birthday.day);
Snip20201217_27.png
结构体作为函数形参
void printfStuent(struct Student);
void printfStuent(struct Student student)
{
printf(" age = %d\n",student.age);
printf(" height = %f\n",student.height);
printf(" sex = %c\n",student.sex);
printf(" weight = %f\n",student.weight);
printf(" name = %s\n",student.name);
printf(" birthday.year = %d\n",student.birthday.year);
printf(" birthday.month = %d\n",student.birthday.month);
printf(" birthday.day = %d\n",student.birthday.day);
}
struct Student student2;
student2.age = 30;
student2.height = 172.5;
student2.sex = '1';
student2.weight = 150.55;
strcpy(student2.name,"liyanyan");
student2.birthday.year = 1990;
student2.birthday.month = 07;
student2.birthday.day = 07;
//传入结构体参数
printfStuent(student2);
结构体指针的声明
struct 结构体名 *指针变量名
//声明一个结构体变量
struct Student *student
结构体指针的声明与初始化
//声明一个结构体变量
struct Student student2;
//1.声明直接初始化
struct Student *student = &student2;
//2.先声明再初始化
struct Student *student3 = NULL;
student3 = &student2;
结构体指针访问成员
结构体指针变量名->成员名
struct Student student2;
//声明直接初始化
struct Student *student = &student2;
student->age = 30;
student->sex = '1';
student->weight = 150.5;
结构体的内存占用
结构体是把不同类型的数据组合成一个整体,其变量是共存的,变量不管是否使用,都会分配内存。
缺点:所有属性都分配内存,比较浪费内存,假设有4个int成员,一共分配了16字节的内存,但是在使用时,你只用一个int成员,也就是使用了4字节,剩余的12字节就是属于内存的浪费。
优点:存储容量较大,包容性强,且成员之间不会相互影响。
网友评论