중첩 된 구조체를 기본값으로 초기화하고 하위 구조체 필드의 특정 필드 만 할당하려고했습니다. 그래서 중첩 된 구조의 모든 내용을 초기화 할 필요는 없습니다.C 중첩 된 구조체의 기본 초기화
아래 샘플 코드에서 볼 수 있듯이 하위 하위 구조의 필드 값을 인쇄하는 동안 세그먼트 화 오류가 발생합니다. 누구든지 각 필드를 거치지 않고 기본값을 초기화하는 더 좋은 방법을 제안합니까? 감사
struct special_char
{
char c;
int size;
};
struct alphabet
{
struct special_char* special_c;
int special_char_size;
};
struct numeral
{
int array_numeral;
int size;
};
struct alpha_numeral
{
struct alphabet alpha;
struct numeral num;
int size;
};
int main()
{
printf(" Initialization of nested structures !\n");
struct alpha_numeral test = { { { 0 } } };
printf("values %d", test.alpha.special_c->size);
return 0;
}
출력 : 중첩 된 구조의 초기화!
분할 오류 (덤프 코어)
C 또는 C++? 하나를 선택하면 대답이 달라집니다. –
'special_char * special_c'에 대해 어디에서 메모리를 할당 했습니까? –
C++로 컴파일되지 않습니다. – jotik