深入理解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 }
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 }
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。