我编写的函数中已经有了一些调试元素。当我输入“y”或“Y”作为输入时,运行时会出现分段错误。当我输入任何其他值代码运行。在扫描并给出响应之后,但在输出“扫描工作”行之前,seg故障会启动。不知道为什么它只会在这些值上起作用。如果任何人需要函数调用,我也有。某些输入上的分段错误
query_user(char *response [10])
{
printf("response after query call before clear=%s\n",response);
strcpy(response,"");
printf("response after clearing before scan=%s\n",response);
printf("Enter another person into the line? y or n\n");
scanf("%s", response);
printf("response after scan=%s\n",response);
printf("scan worked");
}
main()
{
char response [10];
strcpy(response,"y");
printf("response=%s\n",response);
printf("When finished with program type \"done\" to exit\n");
while (strcmp(response,"done") != 0)
{
printf("response after while loop and before query call=%s\n",response);
query_user(&response);
}
}
输出上的错误:在非错误
response after query call before clear=y response after clearing before scan= Enter another person into the line? y or n y response after scan=y Segmentation Fault (core dumped)
输出:
response after query call before clear=y response after clearing before scan= Enter another person into the line? y or n n response after scan=n scan worked Cycle number 0 (program continues to run outside this function)
你为什么要将10个指针的数组传递给'query_user'?我不认为这就是你想要做的。您还应该显示调用该函数的代码以及文本缓冲区的内存分配。 –
main(){char response [10]; \t strcpy(response,“y”); printf(“response =%s \ n”,response); (“完成程序类型\”完成\“退出\ n”); (strcmp(response,“done”)!= 0)printf(“while循环之前和查询之前的响应=%s \ n”,响应); \t \t query_user(&response); –