循环第二次运行中的策略失败
由于某种原因,我会遇到segmation Fail(核心转储),但仅在循环中的第二次运行中。某些背景PTR在main()中被声明为char*,并将其初始化为null。 附加代码和打印的屏幕截图
char* get_command(char **str) {
char c;
int i,add=0;
printf("\tget command\n");
for(i=0; (c=getchar())!= '\n';i++) {
if(i==TOTAL_CHAR*add){
*str = (char*)realloc(*str,sizeof(char)*TOTAL_CHAR*
(++add));
printf("\tmemory alocate succede\n");
if (*str==NULL) {/*warning if realloc has failed*/
fprintf(stderr,"ERROR:: realloc fail");
exit(0);
}
}
**(str+i)=c;
printf("\tchar should be: %c, actual: %c\n",c,**(str+i));
}
return *str;
}
for some reason I get a segmation fail(core dump) but only on the second run in the loop. some background ptr is declared as char* in main() and initialized as NULL.
Attaching screenshot of the code and prints
char* get_command(char **str) {
char c;
int i,add=0;
printf("\tget command\n");
for(i=0; (c=getchar())!= '\n';i++) {
if(i==TOTAL_CHAR*add){
*str = (char*)realloc(*str,sizeof(char)*TOTAL_CHAR*
(++add));
printf("\tmemory alocate succede\n");
if (*str==NULL) {/*warning if realloc has failed*/
fprintf(stderr,"ERROR:: realloc fail");
exit(0);
}
}
**(str+i)=c;
printf("\tchar should be: %c, actual: %c\n",c,**(str+i));
}
return *str;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
**(str+i)= c;
它需要为*(*str+i)= c;
https://godbolt.org/z/t4pmtjn3s
**(str+i)=c;
it needs to be*(*str+i)=c;
https://godbolt.org/z/T4PMTjn3s