熬夜整理的C语言/C++万字总结(四)

1、结构体


1.1 结构体基础知识


1.1.1 结构体类型的定义


struct Person{
 char name[64];
 int age;
};
typedef struct _PERSON{
 char name[64];
 int age;
}Person;


注意:定义结构体类型时不要直接给成员赋值,结构体只是一个类型,编译器还没有为其分配空间,只有根据其类型定义变量时,才分配空间,有空间后才能赋值。



1.1.2 结构体变量的定义


struct Person{
 char name[64];
 int age;
}p1; //定义类型同时定义变量
struct{
 char name[64];
 int age;
}p2; //定义类型同时定义变量
struct Person p3; //通过类型直接定义


1.1.3 结构体变量的初始化


struct Person{
 char name[64];
 int age;
}p1 = {"john",10}; //定义类型同时初始化变量
struct{
 char name[64];
 int age;
}p2 = {"Obama",30}; //定义类型同时初始化变量
struct Person p3 = {"Edward",33}; //通过类型直接定义


1.1.4 结构体成员的使用


struct Person{
 char name[64];
 int age;
};
void test(){
 //在栈上分配空间
 struct Person p1;
 strcpy(p1.name, "John");
 p1.age = 30;
 //如果是普通变量,通过点运算符操作结构体成员
 printf("Name:%s Age:%d\n", p1.name, p1.age);
 //在堆上分配空间
 struct Person* p2 = (struct Person*)malloc(sizeof(struct Person));
 strcpy(p2->name, "Obama");
 p2->age = 33;
 //如果是指针变量,通过->操作结构体成员
 printf("Name:%s Age:%d\n", p2->name, p2->age);
}


1.1.5 结构体赋值


1.1.5.1 赋值基本概念


相同的两个结构体变量可以相互赋值,把一个结构体变量的值拷贝给另一个结构体,这两个变量还是两个独立的变量。


struct Person{
 char name[64];
 int age;
};
void test(){
 //在栈上分配空间
 struct Person p1 = { "John" , 30};
 struct Person p2 = { "Obama", 33 };
 printf("Name:%s Age:%d\n", p1.name, p1.age);
 printf("Name:%s Age:%d\n", p2.name, p2.age);
 //将p2的值赋值给p1
 p1 = p2;
 printf("Name:%s Age:%d\n", p1.name, p1.age);
 printf("Name:%s Age:%d\n", p2.name, p2.age);
}


1.1.5.1 深拷贝和浅拷贝


//一个老师有N个学生
typedef struct _TEACHER{
 char* name;
}Teacher;
void test(){
 Teacher t1;
 t1.name = malloc(64);
 strcpy(t1.name , "John");
 Teacher t2;
 t2 = t1;
 //对手动开辟的内存,需要手动拷贝
 t2.name = malloc(64);
 strcpy(t2.name, t1.name);
 if (t1.name != NULL){
  free(t1.name);
  t1.name = NULL;
 }
 if (t2.name != NULL){
  free(t2.name);
  t1.name = NULL;
 }
}
1.1.6 结构体数组
struct Person{
 char name[64];
 int age;
};
void test(){
 //在栈上分配空间
 struct Person p1[3] = {
  { "John", 30 },
  { "Obama", 33 },
  { "Edward", 25}
 };
 struct Person p2[3] = { "John", 30, "Obama", 33, "Edward", 25 };
 for (int i = 0; i < 3;i ++){
  printf("Name:%s Age:%d\n",p1[i].name,p1[i].age);
 }
 printf("-----------------\n");
 for (int i = 0; i < 3; i++){
  printf("Name:%s Age:%d\n", p2[i].name, p2[i].age);
 }
 printf("-----------------\n");
 //在堆上分配结构体数组
 struct Person* p3 = (struct Person*)malloc(sizeof(struct Person) * 3);
 for (int i = 0; i < 3;i++){
  sprintf(p3[i].name, "Name_%d", i + 1);
  p3[i].age = 20 + i;
 }
 for (int i = 0; i < 3; i++){
  printf("Name:%s Age:%d\n", p3[i].name, p3[i].age);
 }
}


1.2 结构体嵌套指针


1.2.1 结构体嵌套一级指针


struct Person{
 char* name;
 int age;
};
void allocate_memory(struct Person** person){
 if (person == NULL){
  return;
 }
 struct Person* temp = (struct Person*)malloc(sizeof(struct Person));
 if (temp == NULL){
  return;
 }
 //给name指针分配内存
 temp->name = (char*)malloc(sizeof(char)* 64);
 strcpy(temp->name, "John");
 temp->age = 100;
 *person = temp;
}
void print_person(struct Person* person){
 printf("Name:%s Age:%d\n",person->name,person->age);
}
void free_memory(struct Person** person){
 if (person == NULL){
  return;
 }
 struct Person* temp = *person;
 if (temp->name != NULL){
  free(temp->name);
  temp->name = NULL;
 }
 free(temp);
}
void test(){
 struct Person* p = NULL;
 allocate_memory(&p);
 print_person(p);
 free_memory(&p);
}


1.2.2 结构体嵌套二级指针

//一个老师有N个学生
typedef struct _TEACHER{
 char name[64];
 char** students;
}Teacher;
 
void create_teacher(Teacher** teacher,int n,int m){
 
 if (teacher == NULL){
  return;
 }
 
 //创建老师数组
 Teacher* teachers = (Teacher*)malloc(sizeof(Teacher)* n);
 if (teachers == NULL){
  return;
 }
 
 //给每一个老师分配学生
 int num = 0;
 for (int i = 0; i < n; i ++){
  sprintf(teachers[i].name, "老师_%d", i + 1);
  teachers[i].students = (char**)malloc(sizeof(char*) * m);
  for (int j = 0; j < m;j++){
   teachers[i].students[j] = malloc(64);
   sprintf(teachers[i].students[j], "学生_%d", num + 1);
   num++;
  }
 }
 
 *teacher = teachers; 
}
 
void print_teacher(Teacher* teacher,int n,int m){
 for (int i = 0; i < n; i ++){
  printf("%s:\n", teacher[i].name);
  for (int j = 0; j < m;j++){
   printf("  %s",teacher[i].students[j]);
  }
  printf("\n");
 }
}
 
void free_memory(Teacher** teacher,int n,int m){
 if (teacher == NULL){
  return;
 }
 
 Teacher* temp = *teacher;
 
 for (int i = 0; i < n; i ++){
  
  for (int j = 0; j < m;j ++){
   free(temp[i].students[j]);
   temp[i].students[j] = NULL;
  }
 
  free(temp[i].students);
  temp[i].students = NULL;
 }
 
 free(temp);
 
}
 
void test(){
 
 Teacher* p = NULL;
 create_teacher(&p,2,3);
 print_teacher(p, 2, 3);
 free_memory(&p,2,3);
}

上一篇:10W+字C语言硬核总结(九),值得阅读收藏!


下一篇:作为计算机专业学生,最应该学习的课程前五位是什么?