37深入理解C指针之---结构体与指针

  一、结构体与指针

    1、结构体的高级初始化、结构体的销毁、结构体池的应用

    2、特征:

      1)、为了避免含有指针成员的结构体指针的初始化复杂操作,将所有初始化动作使用函数封装;

      2)、封装函数主要实现内存的分配和成员的初始化;

      3)、为了避免含有指针成员的结构体指针的释放内存复杂操作,将所有初始化动作使用函数封装;

      4)、封装函数主要实现分配内存的释放;

      5)、避免频繁的malloc和free的任务开销,一般使用结构体池技术;

    3、结构体的高级初始化应用:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4
 5 typedef struct _student{
 6     char *name;
 7     char *address;
 8     int age;
 9     short id;
10     char sex;
11 } Student;
12
13 void initializeStudent(Student *student, const char *nameg, const char *addressg, const int ageg, const short idg, const char sexg){
14     student->name = (char *)malloc(strlen(nameg) + 1);
15     strcpy(student->name, nameg);
16     student->address = (char *)malloc(strlen(addressg) + 1);
17     strcpy(student->address, addressg);
18     student->age = ageg;
19     student->id = idg;
20     student->sex = sexg;
21
22     return;
23 }
24
25 void displayStudent(Student *student){
26     printf("student %s info:\n", student->name);
27     printf("student.name: %s\n", student->name);
28     printf("student.address: %s\n", student->address);
29     printf("student.age: %d\n", student->age);
30     printf("student.id: %d\n", student->id);
31     printf("student.sex: %c\n", student->sex);
32     printf("\n");
33
34     return;
35 }
36
37 void deallocateStudent(Student *student){
38     free(student->name);
39     free(student->address);
40
41     return;
42 }
43
44 int main(int argc, char **argv)
45 {
46     Student student;
47     initializeStudent(&student, "zhangsan", "jiangxijiujiang", 20, 1102, 'M');
48     displayStudent(&student);
49     deallocateStudent(&student);
50
51     return 0;
52 }

    代码说明:


      1)、第5-11行是结构体定义

      2)、函数initializeStudent()主要完成结构体的初始化,使用时只要根据参数的类别和顺序传入合适的参数即可;

      3)、函数deallocateStudent()主要完成结构体的销毁工作,使用时传入结构体变量即可;

      4)、函数displayStudent()主要是实现结构体内容的格式化输出;

      5)、函数main完成对以上3个函数的调用及测试,其中第46行是对结构体变量的声明;

      6)、测试函数中,若声明的是结构体指针变量时,将测试部分换成如下代码段即可;

45 int main(int argc, char **argv)
46 {
47     Student *student = (Student *)malloc(sizeof(Student));
48     initializeStudent(student, "zhangsan", "jiangxijiujiang", 20, 1102, 'M');
49     displayStudent(student);
50     deallocateStudent(student);
51
52     return 0;
53 }

 

    
    4、结构体的销毁应用:

      1)、函数deallocateStudent()的具体实现;

      2)、释放内存的顺序与分配的顺序完全相反即可;

    5、结构体池技术的应用:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #define LIST_SIZE 10
 5
 6 typedef struct _student{
 7     char *name;
 8     char *address;
 9     int age;
10     short id;
11     char sex;
12 } Student;
13
14 void initializeStudent(Student *student, const char *nameg, const char *addressg, const int ageg, const short idg, const char sexg){
15     student->name = (char *)malloc(strlen(nameg) + 1);
16     strcpy(student->name, nameg);
17     student->address = (char *)malloc(strlen(addressg) + 1);
18     strcpy(student->address, addressg);
19     student->age = ageg;
20     student->id = idg;
21     student->sex = sexg;
22
23     return;
24 }
25
26 void displayStudent(Student *student){
27     printf("student %s info:\n", student->name);
28     printf("student.name: %s\n", student->name);
29     printf("student.address: %s\n", student->address);
30     printf("student.age: %d\n", student->age);
31     printf("student.id: %d\n", student->id);
32     printf("student.sex: %c\n", student->sex);
33     printf("\n");
34
35     return;
36 }
37
38 void deallocateStudent(Student *student){
39     free(student->name);
40     free(student->address);
41
42     return;
43 }
44
45 Student *listStu[LIST_SIZE];
46
47 void initializeListStu(){
48     for(int i = 0; i < LIST_SIZE; i++){
49         listStu[i] = NULL;
50     }
51 }
52
53 Student *getStudent(){
54     for(int i = 0; i < LIST_SIZE; i++){
55         if(listStu[i] != NULL){
56             Student *stu = listStu[i];
57             listStu[i] = NULL;
58
59             return stu;
60         }
61     }
62     Student *student = (Student *)malloc(sizeof(Student));
63
64     return student;
65 }
66
67 Student *returnStudent(Student *student){
68     for(int i = 0; i < LIST_SIZE; i++){
69         if(listStu[i] == NULL){
70             listStu[i] = student;
71
72             return student;
73         }
74     }
75     deallocateStudent(student);
76     free(student);
77
78     return NULL;
79 }
80
81 int main(int argc, char **argv)
82 {
83     initializeListStu();
84     Student *student;
85     student = getStudent();
86
87     initializeStudent(student, "zhangsan", "jiangxijiujiang", 20, 1102, 'M');
88     displayStudent(student);
89     returnStudent(student);
90
91     return 0;
92 }

    代码说明:

      1)、第45行声明结构体Student指针数组,数组大小使用宏定义;

      2)、函数initializeListStu()完成数组的初始化;

      3)、函数getStudent()完成从数组中取得结构体指针,若数组中所有指针都为NULL,临时分配空间,并返回指针;

      4)、函数returnStudent()完成将不需要的结构体指针存回到结构体指针数组中,若数组已满,将指针释放;

      5)、第83行代码完成结构体指针数组的初始化;

      6)、第84行代码声明结构体指针;

      7)、第85行代码完成从结构体指针数组中获取结构体指针;

      8)、第87行代码完成结构体指针的初始化;

      9)、第88行代码完成结构体指针内容的打印输出;

      10)、第89行代码完成结构体指针的返还,如果结构体指针数组已满,自动释放内存,否则,存回结构体指针数组中;

posted @ 2017-06-08 06:41  叕叒双又  阅读(583)  评论(0编辑  收藏  举报