1

cで新しい構造体を初期化しようとしました。

構文の何が問題になっていますか?

    AddressItem_Callback_ContextType *context;

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

          context = {pEntity->iID, pEntity->cBigIcon};
          //context->Icon = pEntity->cBigIcon;
          //context->iID = pEntity->iID;

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

これについても構文エラーが発生します。

    AddressItem_Callback_ContextType *context = {0,NULL};

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

          //context = {pEntity->iID, pEntity->cBigIcon};
          context->Icon = pEntity->cBigIcon;
          context->iID = pEntity->iID;

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

   if (pEntity->cSmallIcon[0] != 0){
      if (res_get(RES_BITMAP,RES_SKIN, pEntity->cSmallIcon) == NULL){

          //context = {pEntity->iID, pEntity->cSmallIcon};
          context->Icon = pEntity->cSmallIcon;
          context->iID = pEntity->iID;

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

次に、これ(3)は機能するはずですか?

 AddressItem_Callback_ContextType context = {0,NULL};

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

          //context = {pEntity->iID, pEntity->cBigIcon};
          context.Icon = pEntity->cBigIcon;
          context.iID = pEntity->iID;

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

   if (pEntity->cSmallIcon[0] != 0){
      if (res_get(RES_BITMAP,RES_SKIN, pEntity->cSmallIcon) == NULL){

          //context = {pEntity->iID, pEntity->cSmallIcon};
          context.Icon = pEntity->cSmallIcon;
          context.iID = pEntity->iID;

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

2 に答える 2

2

ouah が言うように、初期化子は宣言時に使用する必要がありますが、C99 では複合リテラルを使用できます。

#include <stdio.h>

struct st {
    int a, b;
};

int main(void)
{
    struct st *t;

    t = &(struct st){1, 2};
    printf("%d %d\n", t->a, t->b);
    return 0;
}

あなたの場合

context = &(struct AddressItem_Callback_ContextType){pEntity->iID, pEntity->cBigIcon};
于 2013-07-03T15:05:31.910 に答える