Segmentation fault on certain inputs and not others

Posted by Brandon Schwandt on Stack Overflow See other posts from Stack Overflow or by Brandon Schwandt
Published on 2012-12-13T10:28:28Z Indexed on 2012/12/13 11:04 UTC
Read the original article Hit count: 195

Filed under:
|
|

Heres a function I wrote that has some debugging elements in it already. When i enter either a "y" or a "Y" as the input I get a segmentation fault during runtime. When I enter any other value the code runs. The seg fault kicks out after it scans and gives me the response but before the "scan worked" line is output. DOn't know why it would act like this only on these values. If anyone needs the function call I have that as well.

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

output on error:

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)

output on non-error:

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)

© Stack Overflow or respective owner

Related posts about c

    Related posts about segmentation-fault