4

I'm trying to assign char* field to char* field, but get this error:

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

how can I fix this? and why is that happening?

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

declarations:

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

2 回答 2

13

您不能分配给数组(如错误消息所述)。而是复制字符串:

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

或者,如果您想在脚上开枪(容易出现缓冲区溢出):

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

或者,如果您想在自己的脚下开枪而没有注意到它(缓冲区溢出安全,但如果字符串太长则不会 NUL 终止):

strncpy(aStruct->member, "the string", sizeof(aStruct->member));
于 2013-07-04T06:48:20.340 回答
-2

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

            context->Icon = pEntity->cBigIcon;

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

尝试这个 ..

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

于 2013-07-04T06:50:10.133 回答