当前位置:网站首页>【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:
边栏推荐
- Unity屏幕截图功能
- Apache2 configuration denies access to the directory, but can access the settings of the files inside
- Bisection (integer bisection and floating point bisection)
- 行业分析| 快对讲,楼宇对讲
- Still using simpledateformat for time formatting? Be careful that the project collapses!
- The development and principle of the metacosmic system
- 无法重新声明块范围变量
- 來吧元宇宙,果然這熱度一時半會兒過不去了
- Day32 JS note event (Part 1) September 27, 2021
- Deployment and optimization of vsftpd service
猜你喜欢

SEO优化的许多好处是与流量有直接关系

Web page tips this site is unsafe solution

网页提示此站点不安全解决方案

js中this的默认指向及如何修改指向 2021.11.09

Redis principle - List

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

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

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

Array method in JS 2021.09.18

Remote login sshd service
随机推荐
js中this的默认指向及如何修改指向 2021.11.09
Day28 strict mode, string JS 2021.09.22
[no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community
来吧元宇宙,果然这热度一时半会儿过不去了
Fruit FL studio/cubase/studio one music host software comparison
Ali three sides: what is the difference between using on or where in the left join associated table and the condition
The development and principle of the metacosmic system
Adding a new user in MySQL 5.7
面试步骤的面试技巧
day31 js笔记 DOM下 2021.09.26
Machine learning project captcha based on verification code recognition_ Trainer operation practice
JS foundation 10
Industry analysis - quick intercom, building intercom
Interview skills for interview steps
Int~long long indicates the maximum and minimum number
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed
Random forest and poetry maker trained by AMR
Oracle date format exception: invalid number
Using soapUI to obtain freemaker's FTL file template