【发布时间】:2017-02-18 20:42:44
【问题描述】:
我想要一个动态的字符串数组,所以是指向数组指针的指针。 这是我的代码(我的程序在打印后崩溃):
typedef struct person{
char *name;
char **children;
struct person *nextPerson;
}Person;
int main( ){
int kidsNum = 1;
int i;
Person *first = (Person*)malloc(sizeof(Person));
first->name = "George";
first->children = malloc(kidsNum * sizeof(char*));
for (i = 0; i < kidsNum; i++){
//every string consists maximum of 80 characters
(first->children)[i] = malloc((80+1) * sizeof(char));
scanf("%s",((first->children)[i]));
printf("%s",(*((first->children))[i]));
}
}
它在 printf 之后崩溃,我不知道它是否因为错误的 mallocing 而崩溃,或者我不知道如何在场景中正确打印字符串。
【问题讨论】:
-
scanf & printf 的参数应该和打印字符数组一样/
-
printf("%s",(*((first->children))[i]));-->printf("%s\n", first->children[i]); -
编译时启用所有警告
标签: c pointers multidimensional-array malloc