- 论坛徽章:
- 0
|
直接代码,子函数第一调用正常执行,第二次调用出现段错误,新手实在不解,请求大神指导
变量定义
int tel_num;
struct st_tel *tel_p = NULL;
主函数多次调用
tel_add(&tel_p, &tel_num);
子函数
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;
//出现段错误
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;
} |
|