#include#include #include typedef struct _Teach* pTeach;typedef struct _Student* pStudent;typedef struct _Teach{ char* name; pStudent* student; }Teach;typedef struct _Student{ char* name; int age;}Student;void freeStruct(pTeach* pArray,int len){ if(pArray != NULL) { int i,j; for(i=0;i name) { free(pArray[i]->name); pArray[i]->name = NULL; } if(NULL != pArray[i]->student) { for(j=0;j<4;j++) { if(NULL != pArray[i]->student[j]) { if(NULL != pArray[i]->student[j]->name) { free(pArray[i]->student[j]->name); pArray[i]->student[j]->name = NULL; } free(pArray[i]->student[j]); pArray[i]->student[j] = NULL; } } free(pArray[i]->student); pArray[i]->student = NULL; } free(pArray[i]); pArray[i]=NULL; } } free(pArray); pArray = NULL; }}void initStruct(pTeach** teacher,int len){ pTeach* pArray = malloc(sizeof(pTeach)*3); *teacher = pArray; int i,j; for(i=0;i<3;i++) { // teacher; pArray[i] = malloc(sizeof(Teach)); pArray[i]->name = malloc(sizeof(char)*64); sprintf(pArray[i]->name,"teacher_%d",i+1); // point array pArray[i]->student = malloc(sizeof(pStudent)*4); for(j=0;j<4;j++) { //point value; pArray[i]->student[j] = malloc(sizeof(Student)); pArray[i]->student[j]->name = malloc(sizeof(char)*64); sprintf(pArray[i]->student[j]->name,"%s_student_%d",pArray[i]->name,j+1); pArray[i]->student[j]->age = i +30; } }}void showStruct(pTeach* pArray,int len){ if(pArray != NULL) { int i,j; for(i=0;i name); for(j=1;j<4;j++) { printf(" student name is : %s,age is :%d\n",pArray[i]->student[j]->name,pArray[i]->student[j]->age); } } } }void test01(){ pTeach* pArray = NULL; initStruct(&pArray,3); showStruct(pArray,3); freeStruct(pArray,3);}int main(){ test01(); return 0;}
程序的输出
[root code]# ./struct_point
teacher_1 student name is : teacher_1_student_1,age is :30 student name is : teacher_1_student_2,age is :30 student name is : teacher_1_student_3,age is :30 student name is : teacher_1_student_4,age is :30 teacher_2 student name is : teacher_2_student_1,age is :31 student name is : teacher_2_student_2,age is :31 student name is : teacher_2_student_3,age is :31 student name is : teacher_2_student_4,age is :31 teacher_3 student name is : teacher_3_student_1,age is :32 student name is : teacher_3_student_2,age is :32 student name is : teacher_3_student_3,age is :32 student name is : teacher_3_student_4,age is :32
程序的内存检测
[root code]# valgrind --leak-check=full ./struct_point
==4840== Memcheck, a memory error detector ==4840== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==4840== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info ==4840== Command: ./struct_point ==4840== teacher_1 student name is : teacher_1_student_1,age is :30 student name is : teacher_1_student_2,age is :30 student name is : teacher_1_student_3,age is :30 student name is : teacher_1_student_4,age is :30 teacher_2 student name is : teacher_2_student_1,age is :31 student name is : teacher_2_student_2,age is :31 student name is : teacher_2_student_3,age is :31 student name is : teacher_2_student_4,age is :31 teacher_3 student name is : teacher_3_student_1,age is :32 student name is : teacher_3_student_2,age is :32 student name is : teacher_3_student_3,age is :32 student name is : teacher_3_student_4,age is :32 ==4840== ==4840== HEAP SUMMARY: ==4840== in use at exit: 0 bytes in 0 blocks ==4840== total heap usage: 34 allocs, 34 frees, 1,140 bytes allocated ==4840== ==4840== All heap blocks were freed -- no leaks are possible ==4840== ==4840== For counts of detected and suppressed errors, rerun with: -v ==4840== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 8)代码内容是通过学习B站上的视频课程编写的, 如有侵权, 请联系我删除.