C语言-指针练习

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

struct Teacher {
	char* name;
	char** students;
};
void mallocSpace(struct Teacher*** teas) {

	struct Teacher** te = (struct Teacher**)malloc(sizeof(struct Teacher*)*3);
	for (int i = 0; i < 3; ++i) {
		te[i] = (struct Teacher*)malloc(sizeof(struct Teacher));
		te[i]->name = (char*)malloc(sizeof(char) * 64);
		sprintf(te[i]->name, "%d_Teacher",i+1);
		te[i]->students = (char**)malloc(sizeof(char*) * 5);
		for (int j = 0; j < 5; ++j) {
			te[i]->students[j] = (char*)malloc(sizeof(char) * 64);
			sprintf(te[i]->students[j], "%d_Teacher_%d_Student", i + 1, j + 1);
		}
	}
	*teas = te;
}
void printSpace(struct Teacher** teas) {
	for (int i = 0; i < 3; ++i) {
		struct Teacher* tea = teas[i];
		printf("%s\n", tea->name);
		for (int j = 0; j < 5; ++j) {
			printf("%s\n", tea->students[j]);
		}
	}
}
void freeSpace(struct Teacher** teas) {
	for (int i = 0; i < 3; ++i) {
		free(teas[i]->name);
		teas[i]->name = NULL;
		for (int j = 0; j < 5; ++j) {
			free(teas[i]->students[j]);
			teas[i]->students[j] = NULL;
		}
		free(teas[i]->students);
		teas[i]->students = NULL;
		free(teas[i]);
		teas[i] = NULL;
	}
	if (teas != NULL) {
		free(teas);
		teas = NULL;
	}
}

int main(int argc, char* argv[])
{
	struct Teacher** teas = NULL;
	mallocSpace(&teas);
	printSpace(teas);
	freeSpace(teas);
	system("pause");
	return EXIT_SUCCESS;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值