Ex-구조체에서 typedef 사용 방법 두가지

CODEDRAGON Development/C, C++

반응형

   

구조체에서 typedef 사용 방법 두가지

  1. 구조체 정의와 동시에 typedef 선언
  2. 구조체 정의와 개별적으로 typedef 선언

   

   

   

소스코드

   

#include <stdio.h>

//
구조체에서 typedef 사용하는 방법 1
typedef struct score{

        double math;
        double english;
        double korean;
        double total;
        double average;
} SCORE;        //
구조체 변수가 아니라, SCORE struct score라는 데이터 타입을 의미




struct student{
        int no;
        SCORE score;                                    // struct score s;
};


//
구조체에서 typedef 사용하는 방법 2
typedef struct student STUDENT;         //STUDENT
struct student라는 데이터 타입을 의미

int main(void){
        STUDENT stu={123456, {90, 80, 70, 0, 0}};       //
중첩구조체 초기화

        stu.score.total=stu.score.math+stu.score.english+stu.score.korean;
        stu.score.average=stu.score.total/3;
        printf("
학번: %d \n", stu.no);
        printf("
총점: %lf \n", stu.score.total);
        printf("
평균: %lf \n", stu.score.average);

        return 0;
}


   

   

출력결과

   

반응형