【问题标题】:Strange struct declaration奇怪的结构声明
【发布时间】:2011-01-14 02:09:58
【问题描述】:

我熟悉 C 中的结构和数组,但是我不知道下面的代码中发生了什么。 struct 声明的顺序通常是:

struct employee {
  char title;
  int year;
} mark;

为什么 struct(下)和 [] 括号后面有 2 个单词?据我所知,它被用作条件、动作查找表。

const struct act_tbl ActionTbl[] =
{
    {0, BUZZ | DISP | A_ONCE},
    {0, BUZZ | DISP | A_ONCE},
};

【问题讨论】:

    标签: c arrays struct constants lookup


    【解决方案1】:

    代码的第一部分,

    struct employee {
      char title;
      int year;
    } mark;
    

    表示“定义一个名为employee 的结构,然后创建一个名为mark 的实例。”相当于更冗长了

    struct employee {
      char title;
      int year;
    };
    struct employee mark;
    

    代码的第二部分,

    const struct act_tbl ActionTbl[]={    {0, BUZZ|  DISP|  A_ONCE},
                                              {0, BUZZ|  DISP|  A_ONCE},
    

    };

    表示“创建一个struct act_tbl 类型的对象数组,其中第一个act_tbl 初始化为{0, BUZZ| DISP| A_ONCE},第二个也初始化为{0, BUZZ| DISP| A_ONCE}。”不过,如果没有更多关于 act_tbl 是什么的知识,我想我无法就它为何如此编写提供更多建议。

    【讨论】:

    • @arch:这意味着 struct act_tbl 在您的代码中的其他地方定义。可能在某个头文件中。
    • 感谢您的回复。它在另一个文件中定义为:struct act_tbl { uint32 U32_Condition; uint32 U32_Action; }; //还有{0, BRK|等更多的条目显示},{0,保持| DISP|}, //知道如何使用它吗?再次感谢
    【解决方案2】:

    看来:

    struct act_tbl
    

    引用已声明为 act_tbl 的(结构)类型。

    const struct act_tbl ActionTbl[]
    

    正在初始化一个名为 ActionTbl[] 的结构的 'const' 形式(其中 [] 表示创建了一个数组)

    { {0, BUZZ| DISP| A_ONCE}, {0, BUZZ| DISP| A_ONCE}, };
    

    是初始化参数。

    【讨论】:

      【解决方案3】:

      struct act_tbl 是类型,ActionTbl 是它声明的变量。

      变量后的[] 表示它是一个数组。由于中间缺少整数,因此数组的大小由初始化器的数量决定。

      【讨论】:

        【解决方案4】:

        他们正在定义和初始化一个 act_tbl 结构数组。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-05
          相关资源
          最近更新 更多