指针异常问题怎么解决求助
程序代码:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> //结构体嵌套二级指针练习 //结构体设计 struct TeaCher { //老师姓名 char *name; //老师带的学生姓名数组 char **Studets; }; void allocateSpace(struct TeaCher ***teacherArray) { if (teacherArray == NULL) { return; } //堆区分配内存 struct TeaCher ** ts = malloc(sizeof(struct TeaCher *) * 3); //数据赋值 for (int i = 0; i < 3; i++) { ts[i] = malloc(sizeof(struct TeaCher));//给老师分配内存 ts[i]->name = malloc(sizeof(char) * 64);//给老师姓名属性分配内存 sprintf(ts[i]->name, "TeaCher_%d", i + 1);//给老师姓名赋值 //给老师带领学生数组分配内存 ts[i]->name = malloc(sizeof(char *)* 4); //给学生姓名赋值 for (int j = 0; j < 4; j++) { ts[i]->Studets[j] = (char)malloc(sizeof(char)* 64); sprintf(ts[i]->Studets[j], "%s_studet_%d", ts[i]->name, j + 1); } } //建立关系 *teacherArray = ts; } //打印操作 void printTeacharArray(struct TeaCher ** teacherArray) { for (int i = 0; i < 3; i++) { printf("%s\n", teacherArray[i]->name);//老师姓名 for (int j = 0; j < 4; j++) { printf(" %s\n",teacherArray[i]->Studets[j]); } } } void test01() { //老师数组创建 struct TeaCher ** teacherArray = NULL; //分配内存 allocateSpace(&teacherArray); //打印所有老师和学生的信息 printTeacharArray(teacherArray); } int main() { test01(); system("pause"); return EXIT_SUCCESS; }