#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;
}
C语言-指针练习
最新推荐文章于 2024-09-13 22:12:17 发布