结构体嵌套二级指针练习

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Teacher
{
	char * name;
	char ** Students;
};


void allocateSpace(struct Teacher *** teachers)
{

	struct Teacher ** pArray = malloc(sizeof(struct Teacher *) * 3);

	for (int i = 0; i < 3; i++)
	{
		//给每个老师分配空间
		pArray[i] = malloc(sizeof(struct Teacher));

		//给每个老师姓名 分配空间
		pArray[i]->name = malloc(sizeof(char) * 64);
		sprintf(pArray[i]->name, "Teacher_%d", i + 1);

		//给老师带的学生的数组分配空间
		pArray[i]->Students = malloc(sizeof(char *) * 4);

		//给4个学生在分配内存,并且赋值
		for (int j = 0; j < 4; j++)
		{
			pArray[i]->Students[j] = malloc(sizeof(char) * 64);
			sprintf(pArray[i]->Students[j], "%s_Student_%d", pArray[i]->name, j + 1);
		}
	}


	*teachers = pArray;
}


void showArray(struct Teacher ** pArray, int len)
{
	for (int i = 0; i < len; i++)
	{
		printf("%s\n", pArray[i]->name);
		for (int j = 0; j < 4; j++)
		{
			printf("     %s\n", pArray[i]->Students[j]);
		}
	}
}

void freeSpace(struct Teacher ** pArray, int len)
{
	for (int i = 0; i < len; i++)
	{
		//释放老师姓名
		if (pArray[i]->name != NULL)
		{
			free(pArray[i]->name);
			pArray[i]->name = NULL;
		}

		//释放每个学生
		for (int j = 0; j < 4; j++)
		{
			if (pArray[i]->Students[j] != NULL)
			{
				free(pArray[i]->Students[j]);
				pArray[i]->Students[j] = NULL;
			}
		}

		//释放学生数组
		free(pArray[i]->Students);
		pArray[i]->Students = NULL;


		//释放老师
		free(pArray[i]);
		pArray[i] = NULL;
	}


	//释放老师数组
	free(pArray);
	pArray = NULL;
}

void test01()
{

	struct Teacher ** pArray = NULL;
	//分配内存
	allocateSpace(&pArray);

	//打印数组
	showArray(pArray, 3);


	//释放内存
	freeSpace(pArray, 3);
	pArray = NULL;

}


int main() {
	test01();


	system("pause");
	return EXIT_SUCCESS;
}

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>


struct Tearcher
{
	char *name;
	char **student;
};

struct Tearcher ** allocateSpace()
{
	struct Tearcher** pArray = malloc(sizeof(struct Tearcher*) * 3);

	for (int i = 0; i < 3; i++) 
	{
		//给每个结构体开辟内存
		pArray[i] = malloc(sizeof(struct Tearcher));


		//给每个老师的姓名 开辟内存
		pArray[i]->name = malloc(sizeof(char) * 64);
		sprintf(pArray[i]->name, "Teacher_%d", i + 1);


		//给老师带的学生的数组分配空间
		pArray[i]->student = malloc(sizeof(char*) * 4);

		//给4个学生在分配内存,并且赋值
		for (int j = 0; j < 4; j++) 
		{
			pArray[i]->student[j] = malloc(sizeof(char) * 64);
			sprintf(pArray[i]->student[j], "Teacher_%d_Student_%d", i+1, j+1);
		}
	}

	return pArray;
}

void printArray(struct Tearcher ** pArray, int len)
{
	for (int i = 0; i < len; i++) 
	{
		printf("老师姓名: %s\n", pArray[i]->name);

		for (int j = 0; j < 4; j++) 
		{
			printf("学生姓名: %s\n", pArray[i]->student[j]);
		}

		printf("\n");
	}
}

void freeSpace(struct Tearcher ** pArray, int len)
{
	for (int i = 0; i < len; i++) 
	{
		if (pArray[i]->name != NULL)
		{
			free(pArray[i]->name);
			pArray[i]->name = NULL;
		}
	}

	for (int i = 0; i < len; i++)
	{
		for (int j = 0; j < 4; j++) 
		{
			if (pArray[i]->student[j] != NULL)
			{
				free(pArray[i]->student[j]);
				pArray[i]->student[j] = NULL;
			}
		}

		if (pArray[i]->student != NULL)
		{
			free(pArray[i]->student);
			pArray[i]->student = NULL;
		}
	}

	if (pArray != NULL) 
	{
		free(pArray);
		pArray = NULL;
	}
}

void test01() 
{
	struct Tearcher ** pArray = NULL;

	pArray = allocateSpace();
	printArray(pArray, 3);

	freeSpace(pArray, 3);
	pArray = NULL;
}

int main() {
	test01();

	system("pause");
	return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值