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

时间:2015-09-03 13:41:38

标签: 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 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);
}

1 个答案:

答案 0 :(得分:2)

首先,

    private static long back_pressed;

    @Override
    public void onBackpressed()
    {
       if (back_pressed + 2000 > System.currentTimeMillis()) {
         super.onBackPressed();
       } else {
         Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
       }
       this.back_pressed = System.currentTimeMillis();
    }

定义了一个 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);

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