-1
我想要一个动态数组的字符串,所以指向数组的指针。 这是我的代码(打印后,我的程序崩溃):配置一个二维数组,试图打印字符串后崩溃
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,或不知如何打印字符串在场景中正确。
的参数应该是相同的打印字符数组/ –
'的printf( “%s” 时,(*((第一代>儿童)) [ - ]''printf(“%s \ n”,first-> children [i]);' – BLUEPIXY
编译启用所有警告 –