当前位置:网站首页>【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:
边栏推荐
- .NET混合开发解决方案24 WebView2对比CefSharp的超强优势
- 买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教
- What is DAPP system development and analytical understanding
- Industry analysis - quick intercom, building intercom
- Self use demo of basic component integration of fluent
- Day29 JS notes 2021.09.23
- 2022 开源软件安全状况报告:超41%的企业对开源安全没有足够的信心
- fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
- Day28 strict mode, string JS 2021.09.22
- Prefix and (2D)
猜你喜欢

Recommended practice sharing of Zhilian recruitment based on Nebula graph

Day37 JS note motion function 2021.10.11

Day39 prototype chain and page Fireworks Effect 2021.10.13

Django -- MySQL database reflects the mapping data model to models

Web page tips this site is unsafe solution

day31 js笔记 DOM下 2021.09.26

Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022

js中的class类模式及语法 2021.11.10

赛尔号抽奖模拟求期望

2022中国信通院首届业务与应用安全发展论坛成功召开!
随机推荐
Int~long long indicates the maximum and minimum number
Allez, Meta - Cosme, comme prévu, cette chaleur ne durera pas longtemps.
The development and principle of the metacosmic system
Excel import / export convenience tool class
ProCAST finite element casting process simulation software
纯纯大怨种!那些年被劝退的考研专业
无法重新声明块范围变量
Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
Bisection (integer bisection and floating point bisection)
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
携手Cigent:群联为SSD主控固件引入高级网络安全防护特性
What method is required for word, PDF and txt files to realize full-text content retrieval?
零基础C语言(一)
Day23 JS notes 2021.09.14
Simulation of the Saier lottery to seek expectation
[sciter]: use of sciter FS module scanning file API and its details
Characteristics of solar wireless LED display
分析list中有无重复数据且重复了几次
Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022
Contract quantitative trading system development | contract quantitative app development (ready-made cases)