c语言——结构体嵌套一级指针
这里用支了二级指针
void test()
{
struct Person **person = NULL;
person = allocateSpace();//分配内存
printPerson(person);//打印Person数组
freeSpace(person);//释放内存
person = NULL;//置空,防止野指针
}
运行
Name:Name_1 Age:100
Name:Name_2 Age:101
Name:Name_3 Age:102
Name:Name_1的内存被释放!
Name:Name_2的内存被释放!
Name:Name_3的内存被释放!
原理
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Person
{
char *name;
int age;
};
//分配内存
struct Person** allocateSpace()
{
struct Person **temp = malloc(sizeof(struct Person *) * 3);
for (int i = 0; i < 3; ++i)
{
temp[i] = malloc(sizeof(struct Person));
temp[i]->name = malloc(sizeof(char)* 64);
sprintf(temp[i]->name, "Name_%d", i + 1);
temp[i]->age = 100 + i;
}
return temp;
}
//打印
void printPerson(struct Person **person)
{
if (NULL == person)
{
return;
}
for (int i = 0; i < 3; ++i)
{
printf("Name:%s Age:%d\n",person[i]->name,person[i]->age);
}
}
//释放内存
void freeSpace(struct Person **person)
{
if (NULL == person)
{
return;
}
for (int i = 0; i < 3; ++i)
{
if (person[i] == NULL)
{
continue;
}
if (person[i]->name != NULL)
{
printf("Name:%s的内存被释放!\n", person[i]->name);
free(person[i]->name);
person[i]->name = NULL;
}
free(person[i]);
person[i] = NULL;
}
free(person);
person = NULL;
}
void test()
{
struct Person **person = NULL;
person = allocateSpace();
printPerson(person);
freeSpace(person);
person = NULL;
}
int main(){
test();
system("pause");
return EXIT_SUCCESS;
}