当前位置:网站首页>【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:
边栏推荐
- Training notice | special training notice on epidemic prevention and security prevention for overseas Chinese funded enterprises, institutions and personnel in 2022
- Use logrotate to automatically cut the website logs of the pagoda
- Day39 prototype chain and page Fireworks Effect 2021.10.13
- Jetpack Compose Desktop 桌面版本的打包和发布应用
- day39 原型链及页面烟花效果 2021.10.13
- SEO优化的许多好处是与流量有直接关系
- 6. calculation index
- Oracle date format exception: invalid number
- Analyze whether there is duplicate data in the list and repeat it several times
- Batch will png . bmp . JPEG format pictures are converted to Jpg format picture
猜你喜欢

If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience

Unity屏幕截图功能

Redis principle - List

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

Why do many people want to change careers as programmers, while some programmers want to change careers as others?

What method is required for word, PDF and txt files to realize full-text content retrieval?

2018 joint examination of nine provinces & Merging of line segment trees

Simulation of the Saier lottery to seek expectation

js中的数组方法 2021.09.18

Fancy features and cheap prices! What is the true strength of Changan's new SUV?
随机推荐
Cohere, a large model company, completed the round B financing of US $125million
Machine learning project captcha based on verification code recognition_ Trainer operation practice
Class pattern and syntax in JS 2021.11.10
买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教
day37 js笔记 运动函数 2021.10.11
js中this的默认指向及如何修改指向 2021.11.09
水果FL Studio/Cubase/Studio one音乐宿主软件对比
Web3安全连载(3) | 深入揭秘NFT钓鱼流程及防范技巧
2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
Research on personalized product search
[sciter]:sciter如何使用i18实现桌面应用多语言切换及其利弊
Daily practice of C language - day 3: calculate the number of occurrences of sub strings of strings
Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
Day34 JS notes regular expression 2021.09.29
6. calculation index
來吧元宇宙,果然這熱度一時半會兒過不去了
Join hands with cigent: group alliance introduces advanced network security protection features for SSD master firmware
[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
Use logrotate to automatically cut the website logs of the pagoda
day23 js笔记 2021.09.14