从类型’char *’分配类型’char ‘时不兼容的类型

我正在尝试将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; 

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

  context->Icon = pEntity->cBigIcon; 

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

试试这个 ..

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

您无法分配给数组(作为错误消息状态)。 复制字符串:

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

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

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

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

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