これが私が書いた関数で、すでにいくつかのデバッグ要素が含まれています。入力として「y」または「Y」のいずれかを入力すると、実行時にセグメンテーション違反が発生します。他の値を入力すると、コードが実行されます。セグメンテーション違反は、スキャンして応答が返ってきた後、「スキャンが機能しました」という行が出力される前に発生します。これらの値に対してのみこのように動作する理由がわかりません。誰かが関数呼び出しを必要とするなら、私もそれを持っています。
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);
}
}
エラー時の出力:
clear=yの前のクエリ呼び出し後の応答 スキャン前にクリアした後の応答= 行に別の人を入力しますか?yまたはn y scan=y後の応答 セグメンテーション違反(コアダンプ)
エラー以外の出力:
clear=yの前のクエリ呼び出し後の応答 スキャン前にクリアした後の応答= 行に別の人を入力しますか?yまたはn n スキャン後の応答=n スキャンが機能しました サイクル番号0 (プログラムはこの関数の外で実行され続けます)