#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
//结构体嵌套二级指针练习
//结构体设计
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] = 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;
}
调试发现一个
这里报错
改动处见注释,供参考:
#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) {
//void allocateSpace(struct TeaCher*** teacherArray) 修改
//if ((*teacherArray) == NULL) 修改
//{
// return;
//}
//堆区分配内存
struct TeaCher* ts = (struct TeaCher*)malloc(sizeof(struct TeaCher) * 3);
//struct TeaCher** ts = malloc(sizeof(struct TeaCher*) * 3); 修改
//数据赋值
for (int i = 0; i < 3; i++)
{
//ts[i] = malloc(sizeof(struct TeaCher));//给老师分配内存 修改
ts[i].name = (char*)malloc(sizeof(char) * 64);//给老师姓名属性分配内存
//ts[i]->name = ()malloc(sizeof(char) * 64); //修改
sprintf(ts[i].name, "TeaCher_%d", i + 1);//给老师姓名赋值
//sprintf(ts[i]->name, "TeaCher_%d", i + 1);//修改
ts[i].Studets = (char**)malloc(sizeof(char*) * 4);//给老师带领学生数组分配内存
//ts[i]->name = malloc(sizeof(char*) * 4); // 修改
//给学生姓名赋值
for (int j = 0; j < 4; j++)
{
ts[i].Studets[j] = (char *)malloc(sizeof(char) * 64);
//ts[i]->Studets[j] = malloc(sizeof(char) * 64); 修改
sprintf(ts[i].Studets[j], "%s_studet_%d", ts[i].name, j + 1);
//sprintf(ts[i]->Studets[j], "%s_studet_%d", ts[i]->name, j + 1); 修改
}
}
//建立关系
(*teacherArray) = ts;
}
//打印操作
void printTeacharArray(struct TeaCher* teacherArray) {
//void printTeacharArray(struct TeaCher** teacherArray) 修改
for (int i = 0; i < 3; i++)
{
printf("%s\n", teacherArray[i].name);//老师姓名
//printf("%s\n", teacherArray[i]->name);
for (int j = 0; j < 4; j++)
{
printf(" %s\n", teacherArray[i].Studets[j]);
//printf(" %s\n", teacherArray[i]->Studets[j]);
}
}
}
void test01() {
//老师数组创建
struct TeaCher* teacherArray = NULL;
//struct TeaCher** teacherArray = NULL; 修改
//分配内存
allocateSpace(&teacherArray);
//打印所有老师和学生的信息
printTeacharArray(teacherArray);
}
int main()
{
test01();
//system("pause");
return EXIT_SUCCESS;
}
35行name改成Studets
遇到此类问题,说明Studets初始化的有问题,你目光往上看2行,就会发现Studets根本没有分配内存,name写了2次
所以Studets就是传说中的野指针