当前位置:网站首页>【C语言】结构体嵌套二级指针的使用
【C语言】结构体嵌套二级指针的使用
2022-06-28 11:36:00 【贾璞】
结构体嵌套二级指针的使用
Note:
对于结构体嵌套二级指针,务必注意在开辟结构体空间后对于结构体内部指针的操控。
以及对结构体内部指针数组(二级指针)指向的指针空间进行开辟空间。
再有就是对于手动开辟的空间进行一一释放,养成良好的Coding习惯!
运行环境:Ubuntu18.04 GNU GCC/CLION
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Teacher
{
char *name;
char **student;
};
void freePArray(struct Teacher **pArray)
{
if (!pArray)
{
return;
}
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (!pArray[i]->student[j])
{
return;
}
//释放student[0][1][2][3]
free(pArray[i]->student[j]);
pArray[i]->student[j] = NULL;
}
//释放student
free(pArray[i]->student);
pArray[i]->student = NULL;
//释放name
free(pArray[i]->name);
pArray[i]->name = NULL;
//释放pArray[i]
free(pArray[i]);
pArray[i] = NULL;
}
}
void test01()
{
//创建结构体数组指针
struct Teacher **pArray = malloc(sizeof(struct Teacher *) * 3);
if (!pArray)
{
return;
}
for (int i = 0; i < 3; ++i)
{
//开辟结构体空间
pArray[i] = malloc(sizeof(struct Teacher));
//开辟结构体成员中name的空间
pArray[i]->name = malloc(sizeof(char) * 21);
//格式化赋值
sprintf(pArray[i]->name, "Teacher_%d", i + 1);
//开辟结构体成员student四个空间
pArray[i]->student = malloc(sizeof(char *) * 4);
//为每一个student开辟空间并赋值
for (int j = 0; j < 4; ++j)
{
pArray[i]->student[j] = malloc(sizeof(char) * 21);
sprintf(pArray[i]->student[j], "Student_%d", j + 1);
}
}
//遍历
for (int i = 0; i < 3; ++i)
{
printf("%s\n", pArray[i]->name);
for (int j = 0; j < 4; ++j)
{
printf("\t%s\n", pArray[i]->student[j]);
}
}
//printf("%p\n", pArray);
freePArray(pArray);
free(pArray);
pArray = NULL;
}
int main(int argc, char const *argv[])
{
test01();
return 0;
}
Picture:
边栏推荐
- MySQL cannot query the maximum value using the max function
- 行业分析| 快对讲,楼宇对讲
- Day31 JS notes DOM 2021.09.26
- Mysql使用max函数查询不到最大值
- IO stream of file and Base64
- For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?
- Self use demo of basic component integration of fluent
- The development and principle of the metacosmic system
- The default point of this in JS and how to modify it to 2021.11.09
- 6.A-B
猜你喜欢

Still using simpledateformat for time formatting? Be careful that the project collapses!

Oracle date format exception: invalid number

The default point of this in JS and how to modify it to 2021.11.09

Day31 JS notes DOM 2021.09.26

day37 js笔记 运动函数 2021.10.11

Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method

Redis 原理 - List

.NET混合开发解决方案24 WebView2对比CefSharp的超强优势

Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community

Unity screenshot function
随机推荐
2022中国信通院首届业务与应用安全发展论坛成功召开!
Day28 strict mode, string JS 2021.09.22
Daily practice of C language - day 4: find the sum of all even numbers within 100
Multi dimensional monitoring: the data base of intelligent monitoring
day29 js笔记 2021.09.23
What method is required for word, PDF and txt files to realize full-text content retrieval?
Bisection (integer bisection and floating point bisection)
Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
Database Series: is there any way to seamlessly upgrade the business tables of the database
Prefix and (one dimension)
Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques
2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
Packaging and publishing application of jetpack compose desktop version
Mutual conversion between mytipartfile and file
AcWing 610. Salary and bonus (implemented in C language)
Web page tips this site is unsafe solution
[sciter]: how sciter uses i18 to realize multi language switching of desktop applications and its advantages and disadvantages
The default point of this in JS and how to modify it to 2021.11.09
Come on, yuanuniverse. Sure enough, the heat won't pass for a while
Is it safe to buy stocks and open an account on the account QR code of the CICC securities manager? Ask the great God for help