2014-02-06 56 views
0

我一直试图在主(尚未调用)之前运行此操作。它说初始化失败。 可能是什么原因?编译器抱怨大括号的数量,但他们似乎没问题。初始化失败的结构

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 
       }   
       } 
}; 
+0

'.membername ='被称为成员初始化语法,要使用它,您需要* members *,而不仅仅是decl。 – WhozCraig

回答

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

您声明struct suit作为struct contain内部的类型,但你永远不声明该类型的变量。 suitt也不是inner都是变量。你可能想是这样

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

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

虽然自tinner基本上是相同的时间,那么你可能想单独声明,作为一个类型,使该类型的tinner变量。