【问题标题】:converting char array to a char constant (c)将 char 数组转换为 char 常量 (c)
【发布时间】:2015-09-03 13:41:38
【问题描述】:

所以我想弄清楚如何将数组更改为常量,但我一直收到这个错误

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

【问题讨论】:

  • arrayconstant??
  • 您是否阅读过您使用过的函数的手册页?
  • 在继续处理字符串之前,您需要阅读有关数组和指针的知识。

标签: c arrays gcc-warning


【解决方案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);

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

【讨论】:

  • 无论我输入什么,它都会退出。
猜你喜欢
  • 2011-06-08
  • 2013-06-06
  • 2016-03-10
  • 2017-03-05
  • 2016-04-07
  • 2012-07-01
  • 2013-12-31
  • 2012-01-16
  • 1970-01-01
相关资源
最近更新 更多