Mallocing二维数组,尝试打印字符串后崩溃

Mallocing二维数组,尝试打印字符串后崩溃,c,pointers,multidimensional-array,malloc,C,Pointers,Multidimensional Array,Malloc,我想要一个字符串的动态数组,所以指针指向数组的指针。 这是我的代码(打印后我的程序崩溃): typedef结构人{ 字符*名称; 儿童; 结构人*nextPerson; }人; int main(){ int kidsNum=1; int i; Person*first=(Person*)malloc(sizeof(Person)); 第一->name=“乔治”; 第一->children=malloc(kidsNum*sizeof(char*); 对于(i=0;i子项)[i]=malloc((

我想要一个字符串的动态数组,所以指针指向数组的指针。 这是我的代码(打印后我的程序崩溃):

typedef结构人{
字符*名称;
儿童;
结构人*nextPerson;
}人;
int main(){
int kidsNum=1;
int i;
Person*first=(Person*)malloc(sizeof(Person));
第一->name=“乔治”;
第一->children=malloc(kidsNum*sizeof(char*);
对于(i=0;i子项)[i]=malloc((80+1)*sizeof(char));
scanf(“%s”)((第一->子项)[i]);
printf(“%s”)(*((第一个->子项))[i]);
}
}

它在printf之后崩溃,我不知道它是否因为错误的mallocing而崩溃,或者我不知道如何在场景中正确打印字符串。

当您取消引用指针时(这就是
((first->children)[I])
得到指针指向的内存值

在您的例子中,
(*((first->children))[i])
是单个字符(即
char
)而不是字符串。尝试将其打印为字符串将导致未定义的行为和可能的崩溃

不要取消对指针的引用:

printf("%s",first->children[i]);

scanf和printf的参数应与print char数组相同/
printf(“%s”,(*((第一->子项))[i])-->
printf(“%s\n”,第一个->子项[i])编译并启用所有警告
printf("%s",first->children[i]);