2014-02-06 6 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 ='은 멤버 초기화 구문이라고하며 이것을 사용하기 위해서는 decl가 아닌 * members *가 필요합니다. – 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 contain 내부 유형으로 struct suit를 선언,하지만 당신은 그 형태의 변수를 선언하지 않았다. suit이나 t 또는 inner도 변수가 아닙니다. 당신은 아마 그때는 아마 별도로 형식으로 그것을 선언하고 해당 유형의 tinner 변수를 만들고 싶어, 기본적으로 같은 시간이다 t 이후 및 inner 비록

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

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

같은 것을 원한다.

관련 문제