【问题标题】:initializing a new struct in c在c中初始化一个新结构
【发布时间】:2013-07-03 14:48:28
【问题描述】:

我试图在 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 );
      }
   }

【问题讨论】:

  • 您需要为其分配内存并使用= 运算符设置每个成员值(就像您在 cmets 中所做的那样)
  • 更正为:AddressItem_Callback_ContextType *context = NULL;
  • @GrijeshChauhan 我一开始就试过了,但遇到了错误

标签: c syntax struct


【解决方案1】:
context = {pEntity->iID, pEntity->cBigIcon};

{} 初始化列表只能在声明时使用,不能在赋值表达式中使用。

您必须将其分解为两个赋值语句(为此您还必须初始化未初始化的context 指针)。

【讨论】:

  • context 是指针而不是结构本身是否重要。我的意思是初始化列表可以与指针一起使用吗?
  • @PaulR 我也有同样的想法,但很困惑。
  • @ouah 当我的if 语句很少时,我应该怎么写?就像我的第二个代码部分一样?
  • @EladBenda AddressItem_Callback_ContextType *context = {0,NULL}; 你也不能这样做。 context 是指向结构对象的指针,而不是结构对象。
  • @EladBenda 为您的结构分配内存:AddressItem_Callback_ContextType *context = malloc(sizeof *context); 并使用赋值语句来初始化/修改结构成员:context->Icon = /*...*/; context->iID = /* ... */;
【解决方案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-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多