[英]converting char array to a char constant (c)
so im trying to figure out how to change an array to a constant but i keep getting a this error所以我试图弄清楚如何将数组更改为常量,但我不断收到此错误
warning: incompatible pointer types passing
'char *[3]' to parameter of type 'char *' [-Wincompatible-pointer-types]
strcpy(input, inputcon);
^~~~~`
here is my code这是我的代码
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);
}
First of all,首先,
char *input[3];
defines an array of char
pointers, which you don't want here.定义的数组
char
指针,你不想在这里。 A simple char
array will do the job.一个简单的
char
数组就可以完成这项工作。 Change it to将其更改为
char input[4] = {0}; //assuming you want yes to be stored,
// reserve space for terminating null
Secondly,其次,
scanf("%s",*input);
should be应该
scanf("%3s",input); //limit the input as per the buffer length
Third,第三,
strcpy(input, inputcon);
is entirely unnecessary, Remove it.完全没有必要,删除它。
Then, you need to replace然后,您需要更换
yn = strcmp(inputcon, no);
with和
yn = strcmp(input, no);
That said, you should change the prompt to ask user to input yes or no .也就是说,您应该更改提示以要求用户输入yes或no 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.