博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
结构体嵌套二级指针
阅读量:6273 次
发布时间:2019-06-22

本文共 3978 字,大约阅读时间需要 13 分钟。

hot3.png

#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站上的视频课程编写的, 如有侵权, 请联系我删除.

转载于:https://my.oschina.net/u/1579560/blog/3043691

你可能感兴趣的文章
【翻译】Sklearn 与 TensorFlow 机器学习实用指南 —— 第11章 训练深层神经网络(下)...
查看>>
SQLflow:基于python开发的分布式机器学习平台, 支持通过写sql的方式,运行spark, 机器学习算法, 爬虫...
查看>>
机器学习可行性与VC dimension
查看>>
Nacos 发布 1.0.0 GA 版本,可大规模投入到生产环境
查看>>
关于ovirt主机即做存储又兼虚拟机主机的官方文档说明
查看>>
grep匹配结尾字符串的特殊情况
查看>>
第三方农资电商平台大丰收获华创资本数亿元C轮融资
查看>>
“虎鲸跳跃” 完成300万美元Pre-A轮融资,投资方为蓝湖资本及险峰长青
查看>>
JSON简介
查看>>
深圳安泰创新完成数千万新一轮融资,贝森资本领投
查看>>
当 Kubernetes 遇到阿里云
查看>>
MongoDB与Java 经典面试题、课程,好资源值得收藏
查看>>
标普全球获准进入中国市场,本土评级机构压力山大!
查看>>
阿里云基础产品技术月刊 2019年1月
查看>>
Go 语言的垃圾回收演化历程:垃圾回收和运行时问题
查看>>
苹果收购硅谷创业公司 Silk Labs,将继续布局 AI 和 IoT
查看>>
Idea开发Tomcat应用的热部署配置
查看>>
docker安装mysql
查看>>
GNOME 3.34 发布计划敲定,正式版将于9月11日推出
查看>>
使用Data Lake Analytics快速分析OSS上的日志文件
查看>>