将字符串分配给char数组时不兼容的类型

时间:2014-08-31 08:20:57

标签: c arrays char

所以这基本上就是我的主题。 (这是针对C)

char score[50];

if (num == 0){
    score = "draw";
    }
else if (num == 1){ 
    score = "100";
    }
else if (num == 2){
    score = "200";
    }

但是当从类型int分配类型char [50]时,我一直收到错误,不兼容的类型。我该如何解决这个错误?

3 个答案:

答案 0 :(得分:2)

使用strcpychar *strcpy(char *dest, const char *src)

#include <string.h>    //include the library containing the function strcpy

if (num == 0){
    strcpy(score, "draw");
}
else if (num == 1){ 
   strcpy(score, "100");
}
else if (num == 2){
    strcpy(score, "200");
}

答案 1 :(得分:1)

将您的类型更改为const char *

const char* score;

if (num == 0){
    score = "draw";
}
else if (num == 1){ 
    score = "100";
}
else if (num == 2){
    score = "200";
}

答案 2 :(得分:1)

无法将数据分配给数组...我们可以将数据分配为个体。在将字符串移动到数组中,使用strcpy函数,如下所示

  char score[50];

   if (num == 0){
   strcpy(score,"draw");
   }
   else if (num == 1){ 
   strcpy(score,"100");
   }
   else if (num == 2){
   strcpy(score,"200");
   }

无法直接复制到数组......