- 論壇徽章:
- 0
|
直接代碼,子函數(shù)第一調(diào)用正常執(zhí)行,第二次調(diào)用出現(xiàn)段錯(cuò)誤,新手實(shí)在不解,請(qǐng)求大神指導(dǎo)
變量定義
int tel_num;
struct st_tel *tel_p = NULL;
主函數(shù)多次調(diào)用
tel_add(&tel_p, &tel_num);
子函數(shù)
int tel_add(struct st_tel **my_tel, int *tnum)
{
(*my_tel) = realloc(*my_tel, (sizeof(struct st_tel)) * (*tnum + 1));
if(NULL == *my_tel){
printf("realloc is error\n");
return -1;
}
my_tel[*tnum]->name = NULL;
//出現(xiàn)段錯(cuò)誤
my_tel[*tnum]->name = malloc(strlen(argv[1]) + 1);
if(NULL == (my_tel[*tnum]->name)){
printf("malloc is error\n");
return -1;
}
strcpy(my_tel[*tnum]->name, argv[1]);
strcpy(my_tel[*tnum]->num , argv[2]);
return 0;
} |
|