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;
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

2 Answers2

13

You can't assign to arrays (as the error message states). Copy the strings instead:

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

Or, if you want to shoot yourself in the foot (prone to buffer overruns):

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

Or, if you want to shoot yourself in the foot and not notice it (safe of buffer overruns, but doesn't NUL-terminate the string if too long):

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

Array names are constant pointers you can not modify them. In this scenario Icon is a constant pointer and

            context->Icon = pEntity->cBigIcon;

here you are trying to modify it, which is not allowed.

Try this ..

strcpy(context->Icon,pEntity->cBigIcon);

Amit Bhaira
  • 1,687
  • 6
  • 18
  • 31
  • 3
    Icon is not so much a 'constant pointer' as 'an array', and you simply can't assign to arrays like that. – Jonathan Leffler Jul 04 '13 at 06:55
  • int Icon[5]; I am not sure but this is what I think compiler do when we declare an array .. int* const Icon=(int*)malloc(5*sizeof(int)); – Amit Bhaira Jul 04 '13 at 07:10
  • 4
    Your understanding of "what compiler does when declaring an array" is incorrect. Array is not a constant pointer and there's no `malloc` involved. When array type decays to pointer type, the result of the decay is an rvalue. Rvalue of pointer type cannot be constant or non-constant. It is just an rvalue and this is why it is non-modifiable. – AnT stands with Russia Jul 04 '13 at 07:23