简体   繁体   English

结构硬编码初始化中的C结构

[英]C struct within a struct hardcoded initialization

What I am doing wrong here for C99: 我在C99做错了什么:

struct chess {
    struct coordinate {
        char piece;
        int alive;
    } pos[3];
}table[3] =
{
    {
      {'Q', (int)1},{'Q', (int)1},{'Q', (int)1},
      {'B', (int)1},{'B', (int)1},{'B', (int)1},
      {'K', (int)1},{'K', (int)1},{'K', (int)1},
    }
};

It gives the error: 它给出了错误:

error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token

I wish to be able to access the data like having a struct within a struct that: 我希望能够访问数据,例如在结构中具有结构:

table[row].pos[column].piece
table[row].pos[column].alive

I tried several combinations , and none seems to work for this case. 我尝试了几种组合 ,似乎没有一种组合适用于这种情况。 I have done previous struct hard coded initialization before that works, but not a struct within a struct as this time. 在此之前我已经完成了之前的struct硬编码初始化,但这次不是结构中的结构。

Any suggestions? 有什么建议么?

Try to enclose char literals in single quotes as stated above and add extra braces to make inner arrays to be initializer lists. 尝试将char文字括在单引号中,如上所述,并添加额外的大括号,使内部数组成为初始化列表。

struct chess
{
   struct coordinate
   {
       char piece;
       int alive;
   } pos[3];
}
table[3] =
{  // table of 3 struct chess instances...
   { // ... start of a struct chess with a single member of coordinate[3]...
      { // ... this is where the  coordinate[3] array starts... 
         // ... and those are the individual elements of the  coordinate[3] array
         {'Q', 1}, {'Q', 1}, {'Q', 1}
       }
    },
    {{{'B', 1}, {'B', 1}, {'B', 1}}},
    {{{'K', 1}, {'K', 1}, {'K', 1}}}
};
struct chess {
    struct coordinate {
        char piece;
        int alive;
    } pos[3];
} table[3] =
{
    {
        .pos = {{ .piece = 'Q', .alive = 1 },
                { .piece = 'Q', .alive = 1 },
                { .piece = 'Q', .alive = 1 }
               }
    },
    {
        .pos = {{ .piece = 'B', .alive = 1 },
                { .piece = 'B', .alive = 1 },
                { .piece = 'B', .alive = 1 }
               }
    },
    {
        .pos = {{ .piece = 'K', .alive = 1 },
                { .piece = 'K', .alive = 1 },
                { .piece = 'K', .alive = 1 }
               }
    }
};

It seems to work. 它似乎工作。 Just be careful about the placement of your braces, and PLEASE try to understand what you are typing. 请小心放置大括号,并尝试了解您输入的内容。 This is how to read the answer : 这是如何阅读答案:

  1. table[3] is an array. table [3]是一个数组。 So the initializer begin by '{' and ends by '};', with each element separated by a ',' 所以初始化器以'{'开头,以'};'开头,每个元素用','分隔
  2. each element of this array is a 'struct chess'. 这个数组的每个元素都是'struct chess'。 So each sub-element begins by '{' and ends by '}' 所以每个子元素都以'{'开头,以'}结尾
  3. in each 'struct chess' you have an array 'pos[3]'. 在每个'struct chess'中你有一个数组'pos [3]'。 So another sub-sub '{' and '}' 所以另一个子子'{'和'}'
  4. each 'pos' is a struct, so there is sub-sub-sub '{' and '}' 每个'pos'都是一个结构,所以有sub-sub-sub'{'和'}'
  5. finally, the fields are here. 最后,这些领域在这里。 Use the C99 initializer list to make your code cleaner. 使用C99初始化程序列表可以使代码更清晰。 And double quotes in C are char*, not char. C中的双引号是char *,而不是char。 Use single-quotes 使用单引号

Advices : 建议:

  • try to minimize the amount of "difficult-to-read" code. 尽量减少“难以阅读”的代码量。 It will serve you 它会为你服务
  • I never use nested structs. 我从不使用嵌套结构。 When needed, separate their codes and create function which will initialize them. 在需要时,将它们的代码分开并创建将初始化它们的函数。
  • CPPReference is useful CPPReference非常有用
  • use good variables name and identifiers for your struct. 为结构使用好的变量名称和标识符。 When i read 'pos', I think it is a position on a chess ((x, y) coordinates with informations). 当我读'pos'时,我认为它是国际象棋((x,y)坐标与信息的位置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM