struct初始化失败

时间:2014-02-06 00:54:10

标签: c struct

我一直试图在主要之前运行它(没有呼叫)。它说初始化失败了。 可能是什么原因?

编译器抱怨大括号的数量,但它们似乎没问题。

struct contain {
   char* a;         
   int allowed;

   struct suit {
      struct t {
         char* option;
         int count;      
      };

      struct inner {
         char* option; 
         int count;      
      };        
   };
};

// initialize
struct contain _vector = { 
    .a = "John",
    .allowed = 1,
    .suit = {
                 .t = {
                    .option = "ON",
                    .count = 7
                 },
                 .inner = {
                    .option = "ON",
                    .count = 7
                 }          
              }
};

2 个答案:

答案 0 :(得分:2)

您需要实际声明内部结构类型的成员。

struct contain {
char* a;         
int allowed;

struct suit {
       struct t {
              char* option;
              int count;      
       } t;

       struct inner {
              char* option; 
              int count;      
       } inner;
} suit;
};

答案 1 :(得分:1)

您将struct suit声明为struct contain中的类型,但您从未声明该类型的变量。 suittinner都不是变量。你可能想要像

这样的东西
struct suit {
   struct t {
          char* option;
          int count;      
   } suit_t;

   struct inner {
          char* option; 
          int count;      
   } suit_inner;        
} suit_object;
};

虽然tinner基本上是同一时间,但您可能希望将其单独声明为类型并生成该类型的tinner个变量