gpt4 book ai didi

将 char 数组转换为 char 常量 (c)

转载 作者:行者123 更新时间:2023-11-30 18:53:39 25 4
gpt4 key购买 nike

所以我试图弄清楚如何将数组更改为常量,但我不断收到此错误

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 指针数组,您在这里不需要它。一个简单的 char 数组就可以完成这项工作。将其更改为

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

也就是说,您应该更改提示以要求用户输入

关于将 char 数组转换为 char 常量 (c),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32377336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com