这个简单的程序有点麻烦。我可以通过将 response[10] 设为全局变量来解决此问题,但我不想这样做。程序测试是否有正确的响应并且可以正常工作,但返回字符串是垃圾:
#include <stdio.h>
#include <string.h>
char *user_string(char *Setting_Type[]);
int main()
{
char *response;
char *test_names[2] = {"Test", "test"};
printf("Enter \"Test\" or \"test\": ");
response = user_string(test_names);
printf("\nCorrect! Your input is: %s\n", response);
return 0;
}
char *user_string(char *Setting_Type[])
{
int loop = 1;
char response[10];
char *response_string;
while(loop = 1)
{
scanf("%s", &response);
response_string = response;
if(strcmp(response_string, Setting_Type[0]) != 0 && strcmp(response_string, Setting_Type[1]) != 0)
printf("\nWrong! Please try again: ");
else
break;
}
return response_string;
}