结构体
1.结构体
1.1结构体概述
目前我们所需的数据类型仅有基本数据类型short int long,float double char。以上人类无法用于描述较为复杂的数据情况。例如:Person 人,无法使用单一类型完成对于当前 Peraon 的信息描述,本身 Person 信息具备一点【复合程度】,包括 ID 姓名,性别,年龄,信息…
通过一定的的手段将数据进行整合用于描述一个完整的数据类型,在 C 语言中采用的方式就是【struct结构体方式】,结构体是面向对象的雏形。
1.2C语言结构体定义格式
使用结构定义关键字:struct
struct 结构体数据类型名称
{
结构体成员变量1【数据类型 变量名】;
结构体成员变量2【数据类型 变量名】;
结构体成员变量3【数据类型 变量名】;
结构体成员变量4【数据类型 变量名】;
};
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 自定义了一个结构体变量,采用多种不同的基本数据类型
// 整合为一个结构体类型,整个结构体类型完整名称 struct person
struct person
{
int id;
char name[32];
short age;
char info[64];
float salary;
};
/*
类型定义 typedef ,可以给予数据类型重新定义名称
size_t ==> typedef undigned long size_t
typedef struct student{..} Student
Student 就是当前结构体的别名,可以直接使用,并且操作更为方便
*/
typedef struct Student
{
int id;
char name[32];
char class_name[16];
int age;
float sorce;
} Student;
int main(int argc, char const *argv[])
{
/*
struct person 是完整的数据类型 p1 是结构体变量
*/
struct person p1;
/*
Student ==> typedef student student 可以认为
Srudent 和 student student 完全一致。
Studnet 是一个数据类型
*/
Student stu1;
return 0;
}
1.3 结构体变量定义和初始化操作
[结构体变量(存储在栈区)]操作结构体内部数据,需要通过
. 运算符
. ==> 的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
int id;
char name[32];
short age;
float score;
} Student;
int main(int argc, char const *argv[])
{
// 定义结构体变量
Student stu;
/*
【结构体变量】操作结构体内部数据,需要通过
. 运算符
. ==> 的
*/
// 赋值操作
stu.id = 23;
// 使用 C 语言字符串拷贝函数
strcpy(stu.name,"James");
stu.age = 39;
stu.score = 25.8F;
// 取值操作
printf("ID : %d\n",stu.id);
printf("Name : %s\n",stu.name);
printf("Age : %d\n",stu.age);
printf("Score : %f\n",stu.score);
return 0;
}
1.4结构体类型堆区定义
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
int id;
char name[32];
short age;
float score;
char class_name[32];
} Student;
int main(int argc, char const *argv[])
{
/*
malloc 申请空间不会对空间进行擦除操作,为了防止野值问题,可以通过
memset 函数对申请空间进行空间擦除。
memset(void * ptr,char ch,size_t size);
用户提供目标空间首地址,同时限制字节个数,将空间的所有数据
替换为目标字符 ch
memset(sru1,0,sizeof(Student));
将 Student 对应的堆区空间进行擦除操作,使用的是【字符集编号为 0 的字符】
该字符的二进制数据为 0000 0000, 相当于整个申请的空间都是 0
*/
Student *stu1 = (Student *)malloc(sizeof(Student));
memset(stu1,0,sizeof(Student));
Student *stu2 = (Student *)calloc(1,sizeof(Student));
stu1->id = 1;
stycpy(stu1->name,"Tom");
stu1->age = 5;
stu1->score = 100.0F;
strcpy(stu1->class_name,"IOT-2023");
stu2->id = 2;
stycpy(stu2->name,"Jerry");
stu2->age = 3;
stu2->score = 10000.0F;
strcpy(stu2->class_name,"IOT-2013");
printf("ID : %d\n",stu1->id);
printf("Name : %s\n",stu1->name);
printf("Age : %d\n",stu1->age);
printf("Score : %f\n",stu1->score);
printf("ClassName : %s\n",stu1->class_name);
printf("\n");
printf("ID : %d\n",stu2->id);
printf("Name : %s\n",stu2->name);
printf("Age : %d\n",stu1->age);
printf("Score : %f\n",stu1->score);
printf("ClassName : %s\n",stu1->class_name);
free(stu2);
free(stu1);
stu1 = NULL;
stu2 = NULL;
return 0;
}
1.5结构体数组【重点】
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
int id;
char name[32];
short age;
} Student;
int main(int argc, char const *argv[])
{
// 定义了一个数组,数组中存储的元素都是Student * 结构体类型指针
Student * student_array[10] = {NULL};
for (int i = 0; i < 10; i++)
{
// 申请内存堆区空间,数据类型强转为 Student *
Student * stu = (Student *)malloc(sizeof(Student));
memset(stu,0,sizeof(Student));
// 赋值必要数据
stu->id = i + 1;
stu->age = i + 5;
strcpy(stu->name,"Tom");
// 数组中存储目标 Student 结构体指针
student_array[i] = stu;
}
// 循环展示数据
for (int i = 0; i < 10; i++)
{
printf("ID : %d,Name : %s,Age : %d\n",
student_array[i]->id,
student_array[i]->name,
student_array[i]->age);
}
// 循环释放 malloc 申请的内存空间
for (int i = 0; i < 10; i++)
{
free(student_array[i]);
student_array[i] = NULL;
}
return 0;
}
1.6结构体嵌套
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct birthday
{
char year[8];
char month[4];
char day[4];
} Birthday;
typedef struct person
{
int id;
char name[32];
// Person 结构体中使用的自定义结构体类型 Brithday 作为成员变量
Birthday birthday;
} Person;
int main(int argc, char const *argv[])
{
Person *p = (Person *)calloc(1,sizeof(Person));
p->id = 1;
strcpy(p->name,"James");
// 通过 Person 赋值内部 Birthday 结构体变量
strcpy(p->birthday.year,"1985");
strcpy(p->birthday.month,"12");
strcpy(p->birthday.day,"30");
printf("ID : %d\n",p->id);
printf("Name : %s\n",p->name);
printf("Year : %s\n",p->birthday.year);
printf("Month : %s\n",p->birthday.month);
printf("Day : %s\n",p->birthday.day);
return 0;
}