Ex-중첩구조체 초기화

CODEDRAGON Development/C, C++

반응형

   

중첩 구조체를 선언하고 동시에 초기화하기

중첩 구조체 초기화 방법 2가지 확인

   

   

소스코드

   

#include <stdio.h>

struct score{
                double math;
                double english;
                double korean;
                double total;
};

struct student{
                int no;
                struct score ss;                //
구조체 변수 ss 구조체 student 멤버 변수로 사용(중첩 구조체)
};

int main(void){
                //
중첩구조체 초기화 방법 1
                struct student stu={1234567, {90, 80, 60, 0}};
                
                //
중첩구조체 초기화 방법 2 - {}(중괄호) 생략 가능
                // struct student stu={1234567, 90, 80, 60, 0};

                stu.ss.total=stu.ss.math+stu.ss.english+stu.ss.korean;
                printf("
학번: %d \n", stu.no);
                printf("
총점: %lf \n", stu.ss.total);

                return 0;
}


   

   

출력결과

 

반응형