从类型'char *'分配类型'char [128]'时不兼容的类型

时间:2013-07-04 06:43:33

标签: c syntax struct

我正在尝试将char *字段分配给char *字段,但是会收到此错误:

incompatible types when assigning to type 'char[128]' from type 'char *'

我该如何解决这个问题?为什么会这样?

    AddressItem_Callback_ContextType *context = (AddressItem_Callback_ContextType *)malloc(sizeof(AddressItem_Callback_ContextType));

   //check if icons need to be downloaded
   if (pEntity->cBigIcon[0] != 0){
      if (res_get(RES_BITMAP,RES_SKIN, pEntity->cBigIcon) == NULL){

          context->Icon = pEntity->cBigIcon;
          context->iID = pEntity->iID;

         res_download(RES_DOWNLOAD_IMAGE, pEntity->cBigIcon, NULL, "",TRUE, 1, addressItem_icon_download_callback, context );
      }
   }

声明:

typedef struct
{
    int  iID;        //  POI Type ID
    int  iExternalPoiServiceID; // Service ID
    int  iExternalPoiProviderID; // Provider ID
    char cBigIcon[MAX_ICON_LENGHT];
    char cSmallIcon[MAX_ICON_LENGHT];
    char cBigPromotionIcon[MAX_ICON_LENGHT];
    char cSmallPromotionIcon[MAX_ICON_LENGHT];
    char cOnClickUrl[MAX_URL_LENGTH];
..
} RTExternalPoiType;

typedef struct
{
    int  iID;        //  POI Type ID
   //int  iExternalPoiServiceID; // Service ID
   // int  iExternalPoiProviderID; // Provider ID
    char Icon[MAX_ICON_LENGHT];
} AddressItem_Callback_ContextType;

2 个答案:

答案 0 :(得分:12)

您无法分配给数组(如错误消息所述)。 复制字符串:

snprintf(aStruct->member, sizeof(aStruct->member), "%s", someString);

或者,如果你想用脚射击(容易发生缓冲超支):

strcpy(aStruct->member, "the string");

或者,如果你想用脚射击并且没有注意到它(缓冲区溢出的安全性,但是如果太长则不能NUL终止该字符串):

strncpy(aStruct->member, "the string", sizeof(aStruct->member));

答案 1 :(得分:-2)

数组名称是常量指针,您无法修改它们。在这种情况下,Icon是一个常量指针

            context->Icon = pEntity->cBigIcon;

这里你试图修改它,这是不允许的。

试试这个..

的strcpy(上下文>图标,pEntity-> cBigIcon);