Ex-구조체 변수로 배열 사용하기

CODEDRAGON Development/C, C++

반응형

   

구조체 변수로 배열 사용하기

   

   

   

메모리 구조

   

   

소스코드

   

#include <stdio.h>

struct student{
                        char no[10];
                        char name[20];
                        double math;            //
수학 점수, 8byte
                        double english;         //
영어 점수, 8byte
                        double korean;          //
국어 점수, 8byte
                        double total;           //
총점, 8byte
                        double average;         //
평균, 8byte
};

int main(void){
                int i=0;

                //
구조체 변수로 배열 사용
                struct student stu[3]={
                        {"123456", "Bob", 90, 80, 70, 0, 0},
                        {"456789", "Jack", 95, 85, 75, 0, 0},
                        {"789123", "Alice", 99, 88, 77, 0, 0}
                };

                for(i=0; i<3; i++){
                        stu[i].total=stu[i].math+stu[i].english+stu[i].korean;
                        stu[i].average=stu[i].total/3;
                        printf("
학번: %s, 이름: %s \n", stu[i].no, stu[i].name);
                        printf("
총점: %lf \n", stu[i].total);
                        printf("
평균: %lf \n", stu[i].average);
                        printf("\n");
                }

                return 0;
}


   

   

   

출력결과

 

반응형