2012-12-13 46 views
-1

我编写的函数中已经有了一些调试元素。当我输入“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) 
+1

你为什么要将10个指针的数组传递给'query_user'?我不认为这就是你想要做的。您还应该显示调用该函数的代码以及文本缓冲区的内存分配。 –

+0

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); –

回答

2

query_user参数的你的宣言是错误的。你已经声明了一个指向char的指针数组。你需要一个简单的字符缓冲区。就像这样:

query_user(char response[]) 

query_user(char* response) 

使用你喜欢哪个。

当你调用该函数,你可以这样说:

query_user(response); 

另外我想指出的是您为main声明不正确。你应该使用

int main(void) 
+0

我会试试看。感谢您的支持,我在格式化时遇到了问题 –

+0

尝试在声明中加入参数并清理了调用。仍然是同样的问题,当y或Y输入但不是n或N时,seg故障。 –

0

你在做什么并不完全安全。

如果用户录制的磁带大于标签大小,会发生什么情况? (可能是Segfault)。

你真的应该使用类似的scanf( “%787-9”,响应)(你将不得不后,清空缓冲区!):

(举例标签大小= 10):

query_user(char *response) 
{ 
    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("%9s", response); 
    clean_buffer(); 
    printf("response after scan=%s\n",response); 
    printf("scan worked"); 
} 

void clean_buffer() 
{ 
    int c; 
    while ((c = getchar()) != '\n' && c != EOF); 
} 

希望它有帮助!

+0

我在那里安装了该保护装置,并在排除故障时将其移除。仍然试图找出为什么错误只来自y或Y输入,而不是n。 –

0

首先,您发布的代码不会重现该问题。我在Linux上测试用gcc 4.6.2和Windows上使用Visual Studio 2010在这两种情况下,你确切的代码我的输出是:

response=y
When finished with program type "done" to exit
response after while loop and before query call=y
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
scan workedresponse after while loop and before query call=y
response after query call before clear=y
response after clearing before scan=
Enter another person into the line? y or n

因此,为了更好地诊断,我们需要的代码,一个完整的工作组,显示该问题。有很多与发布代码的警告(不知道这些都是在你的基地也一样),有时忽略了警告,可以让你在一个不好的地方:

  • main应该返回intint main()
  • 认沽在结束return 0;
  • query_user在主返回需要void返回类型:void query_user()
  • strcpy需要char *你给它一个char **

对于最后一点,你可以通过数组传递给你的函数,如修复:

query_user(response); // remove the & operator 

和你的query_user()需求标题来更改,如:

query_user(char response [10]) // remove the * operator 

而且请注意,scanf会在stdin上留下一个'\n'字符,以确保您不会消耗掉可以在%s之前放置单个空格:

scanf(" %s", response); 

我不认为任何这些都会产生很大的影响,但请确保您进行了更改。请发布更新到您的代码,实际显示错误。

+0

我试图发表进一步的代码,但它不会让我,一直告诉我这是错误格式。 –