将字符数组转换为字符常量(c)
因此,我试图找出如何将数组更改为常量,但我不断地发现这个错误将字符数组转换为字符常量(c),c,arrays,gcc-warning,C,Arrays,Gcc Warning,因此,我试图找出如何将数组更改为常量,但我不断地发现这个错误 warning: incompatible pointer types passing 'char *[3]' to parameter of type 'char *' [-Wincompatible-pointer-types] strcpy(input, inputcon); ^~~~~` 这是我的密码 int main(void) { char *input[3]; int y
warning: incompatible pointer types passing
'char *[3]' to parameter of type 'char *' [-Wincompatible-pointer-types]
strcpy(input, inputcon);
^~~~~`
这是我的密码
int main(void) {
char *input[3];
int yn = 0;
char *no = "no";
char *inputcon = NULL;
do {
printf("This is the game.\nDo you want to play again?\nType y/n: ");
scanf("%s",*input);
strcpy(input, inputcon);
yn = strcmp(inputcon, no);
} while (yn == 1);
}
首先,
char *input[3];
定义一个字符指针数组,这里不需要。一个简单的字符数组将完成这项工作。换成
char input[4] = {0}; //assuming you want yes to be stored,
// reserve space for terminating null
第二,
scanf("%s",*input);
应该是
scanf("%3s",input); //limit the input as per the buffer length
第三,
strcpy(input, inputcon);
完全没有必要,请将其删除
然后,你需要替换
yn = strcmp(inputcon, no);
与
也就是说,您应该将提示更改为要求用户输入是或否。数组为常量??您是否阅读了所使用函数的手册页?在继续字符串处理之前,您需要阅读有关数组和指针的内容。无论我键入什么,它仍然存在。
yn = strcmp(input, no);