C Programming 복합데이터유형 (Composite Data Types) Seo, Doo-Ok Clickseo.com clickseo@gmail.com
목 차 구조체 구조체와포인터그리고함수 공용체와열거형 2
구조체 구조체 구조체배열 중첩구조체 구조체와포인터그리고함수 공용체와열거형 3
구조체 (Structure) 구조체 (1/8) 서로연관된원소들의집합을하나의이름으로묶어놓은것 변수들의집합체 ( 단, 변수들은서로다른데이터타입을가질수도있다.) 배열과구조체의차이 구조체에있는원소들은같은형식이거나다른형식일수있다. name kor eng math tot ave stu 4
구조체선언및정의 구조체 (2/8) C 언어에서구조체선언및정의 1. 구조체변수 (structure variable) 2. 태그구조체 (tagged structure) 3. 형식정의된구조체 (type-defined structure) // 구조체변수 (structure variable) struct // 태그구조체 (tagged structure) struct tagname } variable_identifier; } variable_identifier; struct tagname variable_identifier; // 형식정의된구조체 (type-defined structure) typedef struct } NEW_TYPE; NEW_TYPE variable_identifier; 5
구조체 (3/8) 구조체선언및정의 : 구조체변수 구조체변수 // 구조체변수 (structure variable) struct struct field-list... } variable_identifier; char int float name[12]; kor, eng, math, tot; ave; }stu; 6
구조체 (4/8) 구조체선언및정의 : 태그구조체 태그구조체 구조체에태그를부여하므로, 이태그를사용해변수나매개변수, 반환값을정의할수있다. // 태그구조체 (tagged structure) struct _score char int float }; struct _score name[12]; kor, eng, math, tot; ave; stu; struct tagname } variable_identifier; struct tagname variable_identifier; void OUTPUT( struct _score temp ); 7
구조체 (5/8) 구조체선언및정의 : 형식정의된구조체 형식정의된구조체 태그구조체과다른점 typedef 키워드를선언부앞에추가하는것 블록문의끝에있는식별자가변수가아닌형식정의이름 typedef struct _score char name[12]; int kor, eng, math, tot; float ave; } SCORE; // 형식정의된구조체 (type-defined structure) typedef struct } NEW_TYPE; SCORE stu; NEW_TYPE variable_identifier; void OUTPUT( SCORE temp ); 8
구조체 (6/8) 구조체초기화 초기값은중괄호를사용하며콤마로분리 단, 구조체에정의된각구조체멤버들의형식과일치해야한다. typedef struct _score char name[12]; int kor, eng, math, tot; float ave; }SCORE; SCORE stu = 서두옥, 70, 80, 90, 240, 80.0}; stu 서두옥 70 80 90 240 80.0 9
구조체 (7/8) 구조체멤버 구조체멤버접근 개별필드참조 : 구두점표기법 (Dot notation) 구조체변수명과멤버이름사이를구두점연산자 (.) 로구분하여사용 stu 서두옥 70 80 90 240 80.0 stu.name stu.kor stu.eng stu.math stu.tot stu.ave 구조체의크기 : 각멤버들이차지하는메모리용량의총합 sizeof ( struct _score ) = 32 Bytes sizeof (SCORE)= 32 Bytes 10
구조체 (8/8) 예제 7-1 : 구조체의이해 #include <stdio.h> typedef struct _score // 구조체형식정의 char name[12]; int kor, eng, math, tot; float ave; } SCORE; int main(void) SCORE stu; // 구조체변수선언 printf(" 학생이름 : "); gets( stu.name ); printf(" 국어점수 : "); scanf("%d", &stu.kor ); printf(" 영어점수 : "); scanf("%d", &stu.eng ); printf(" 수학점수 : "); scanf("%d", &stu.math ); stu.tot = stu.kor + stu.eng + stu.math; stu.ave = (float)stu.tot / 3; printf("\n%10s %5d %5d %5d %5d %8.2f \n\n", stu.name, stu.kor, stu.eng, stu.math, stu.tot, stu.ave ); } return 0; 11
구조체배열 (1/3) 구조체배열 typedef struct _score char name[12]; int kor, eng, math, tot; float ave; }SCORE; SCORE arr[3]; arr[0] arr[1] arr[2] 홍길동 70 80 90 240 80.0 서두옥 70 60 80 210 70.0 서하은 90 80 100 270 90.0 arr 12
#include <stdio.h> 구조체배열 (2/3) 예제 7-2 : 구조체배열 (1/2) #define stumaxsize 3 typedef struct _score char name[12]; int kor, eng, math, tot; float ave; } SCORE; 13
구조체배열 (3/3) 예제 7-2 : 구조체배열 (2/2) int main(void) SCORE arr[stumaxsize]; printf("%d 명의학생성적을입력하시오... \n", stumaxsize ); for (int i = 0; i<stumaxsize; i++) printf("\n%d 번째학생성적입력... \n", i + 1); printf(" 학생이름 : "); gets( arr[i].name ); printf(" 국어점수 : "); scanf("%d", &arr[i].kor ); printf(" 영어점수 : "); scanf("%d", &arr[i].eng ); printf(" 수학점수 : "); scanf("%d%*c", &arr[i].math ); } arr[i].tot = arr[i].kor + arr[i].eng + arr[i].math; arr[i].ave = (float)arr[i].tot / 3; } printf("\n\t ### 성적결과 ### \n\n"); for (int i = 0; i<stumaxsize; i++) printf("%3d : %10s %3d %3d %3d %5d %8.2f \n", i + 1, arr[i].name, arr[i].kor, arr[i].eng, arr[i].math, arr[i].tot, arr[i].ave ); return 0; 14
중첩구조체 중첩구조체 typedef struct struct int year; int month; int day; } date; struct int hour; int min; int sec; } time; } STAMP; 추천하지않는방식 typedef struct int year; int month; int day; } DATE; typedef struct int hour; int min; int sec; } TIME; typedef struct DATE date; TIME time; } STAMP; 선호하는방식 STAMP stamp; STAMP stamp; 15
구조체와포인터그리고함수 구조체 구조체와포인터그리고함수 구조체와포인터 구조체와함수 공용체와열거형 16
구조체와포인터 (1/2) 간접표기법 (Indirection notation) stu.name (*pstu). name typedef struct _score char name[12]; int kor, eng, math, tot; float ave; }SCORE; SCORE SCORE stu; *pstu = &stu; pstu stu (*pstu).eng (*pstu).tot (*pstu).name (*pstu).kor (*pstu).math (*pstu).ave 서두옥 70 80 90 240 80.0 stu.name stu.kor stu.eng stu.math stu.tot stu.ave 17
구조체와포인터 (2/2) 선택표기법 (Selection notation) stu.name pstu->name typedef struct _score char name[12]; int kor, eng, math, tot; float ave; }SCORE; SCORE SCORE stu; *pstu = &stu; pstu stu pstu->eng pstu->tot pstu->name pstu->kor pstu->math pstu->ave 서두옥 70 80 90 240 80.0 stu.name stu.kor stu.eng stu.math stu.tot stu.ave 18
구조체와함수 (1/10) 함수에서구조체멤버접근방법 1. 구조체멤버를함수에전달하고 (pass by value), 함수내부에서일반변수처럼접근한다. 2. 전체구조체를함수에전달하고 (pass by value), 함수내부에서각멤버들을일반변수처럼접근한다. 3. 구조체또는멤버의주소를함수에전달하고 (pass by address), 함수내부에서간접접근방법 ( 간접표기법또는선택표기법 ) 을이용하여구조체멤버를접근한다. 19
구조체와함수 (2/10) 구조체멤버를함수에전달 stu.tot = SUM( stu.kor, stu.eng, stu.math ); c int 90 b int 80 SUM 함수스택영역 a int 70 stu 서두옥 70 80 90?????? stu.name stu.kor stu.math stu.ave main 함수스택영역 stu.eng stu.tot 20
#include <stdio.h> typedef struct _score char name[12]; int kor, eng, math, tot; float ave; } SCORE; int SUM( int, int, int ); float AVE( int, int, int ); int main(void) SCORE stu; 구조체와함수 (3/10) 예제 7-3 : 구조체멤버를함수에전달 -- pass by value (1/2) printf(" 학생성적을입력하시오... \n\n"); printf(" 학생이름 : "); gets(stu.name); printf(" 국어점수 : "); scanf("%d", &stu.kor ); printf(" 영어점수 : "); scanf("%d", &stu.eng ); printf(" 수학점수 : "); scanf("%d%*c", &stu.math ); 21
구조체와함수 (4/10) 예제 7-3 : 구조체멤버를함수에전달 -- pass by value (2/2) stu.tot = SUM( stu.kor, stu.eng, stu.math ); stu.ave = AVE( stu.kor, stu.eng, stu.math ); printf("\n\t ### 입력정보 ### \n\n"); printf("%10s %3d %3d %3d %5d %8.2f \n", stu.name, stu.kor, stu.eng, stu.math, stu.tot, stu.ave ); } return 0; int SUM( int a, int b, int c ) return a + b + c; } float AVE( int a, int b, int c ) return (float)(a + b + c) / 3; } 22
구조체와함수 (5/10) 전체구조체를함수에전달 OUTPUT( stu ); SCORE temp 서두옥 70 80 90?????? temp.name temp.kor temp.math temp.ave temp.eng temp.tot OUTPUT 함수스택영역 SCORE stu 서두옥 70 80 90?????? a.name stu.kor stu.math stu.ave stu.eng stu.tot main 함수스택영역 23
#include <stdio.h> typedef struct _score char name[12]; int kor, eng, math, tot; float ave; } SCORE; SCORE INPUT(void); void OUTPUT( SCORE ); 구조체와함수 (6/10) 예제 7-4 : 전체구조체를함수에전달 -- pass by value (1/2) int main(void) SCORE stu; stu = INPUT(); OUTPUT( stu ); } return 0; 24
구조체와함수 (7/10) 예제 7-4 : 전체구조체를함수에전달 -- pass by value (2/2) SCORE INPUT(void) SCORE temp; printf(" 학생성적을입력하시오... \n\n"); printf(" 학생이름 : "); gets( temp.name ); printf(" 국어점수 : "); scanf("%d", &temp.kor ); printf(" 영어점수 : "); scanf("%d", &temp.eng ); printf(" 수학점수 : "); scanf("%d", &temp.math ); temp.tot = temp.kor + temp.eng + temp.math; temp.ave = (float)temp.tot / 3; } return temp; void OUTPUT( SCORE temp ) printf("\n\t ### 입력정보 ### \n\n"); printf("%10s %3d %3d %3d %5d %8.2f \n", temp.name, temp.kor, temp.eng, temp.math, temp.tot, temp.ave ); } return; 25
구조체와함수 (8/10) 구조체멤버를함수에전달 OUTPUT( &stu ); SCORE * pstu OUTPUT 함수스택영역 SCORE stu 서두옥 70 80 90?????? stu.name stu.kor stu.math stu.ave stu.eng stu.tot main 함수스택영역 26
#include <stdio.h> typedef struct _score char name[12]; int kor, eng, math, tot; float ave; } SCORE; 구조체와함수 (9/10) 예제 7-5 : 구조체의주소를함수에전달 -- pass by address (1/2) void INPUT( SCORE * ); void OUTPUT( SCORE * ); int main(void) SCORE stu; INPUT( &stu ); OUTPUT( &stu ); } return 0; 27
구조체와함수 (10/10) 예제 7-5 : 구조체의주소를함수에전달 -- pass by address (2/2) void INPUT( SCORE *pstu ) printf(" 학생성적을입력하시오... \n\n"); printf(" 학생이름 : "); gets(pstu->name); printf(" 국어점수 : "); scanf("%d", &pstu->kor); printf(" 영어점수 : "); scanf("%d", &pstu->eng); printf(" 수학점수 : "); scanf("%d", &pstu->math); pstu->tot = pstu->kor + pstu->eng + pstu->math; pstu->ave = (float)pstu->tot / 3; } return; void OUTPUT( SCORE *pstu ) printf("\n\t ### 입력정보 ### \n\n"); printf("%10s %3d %3d %3d %5d %8.2f \n", pstu->name, pstu->kor, pstu->eng, pstu->math, pstu->tot, pstu->ave); } return; 28
공용체와열거형 구조체 구조체와포인터그리고함수 공용체와열거형 공용체 열거형 29
공용체 (Union Type) 공용체 (1/2) 메모리에서서로다른데이터형식을공유할수있는개념 공용체는구조형과같은문법양식을따른다. struct 키워드대신 union 키워드를쓰는것외에는모두똑같다. 공용체를참조하는규칙은구조형을참조하는것과동일하다. typedef union char ch; int i; double d; }TYPE; 1000 1001 1002 1003 1004 1005 1006 1007 TYPE temp; temp.ch temp.i temp.d 30
공용체 (2/2) 예제 7-6 : 공용체 #include <stdio.h> typedef union char ch; int i; double d; } TYPE; int main(void) TYPE temp; temp.i = 0x12345678; printf("temp.ch : %#x \n", temp.ch ); printf("temp.i : %#x \n", temp.i ); printf("temp.d : %#I64x \n", temp.d ); } return 0; 31
열거형 (1/4) 열거형 (Enumerated Type) 정수형에기반 열거형에서각정수값은열거상수라고불리는식별자 목적 : 정수에이름을할당하는것 // 열거화된변수형 enum 열거형상수,. } 변수식별자 ; // 열거화된태그형 enum tag 열거형상수, }; enum tag 변수식별자 ; 32
열거형 (2/4) 열거형의정의 : 태그형, 재정의형 // 열거화된태그형 enum Color RED, WHITE, BLUE, GREEN, YELLOW }; enum Color acolor; // 재정의된열거형 typedef enum RED, WHITE, BLUE, GREEN, YELLOW } COLOR; COLOR acolor; 33
열거형의정의 : 초기화 열거형 (3/4) enum months jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec}; 0 부터시작하여각열거상수에정수값부여 enum months jan =1, feb =2, mar = 3, apr = 4, may = 5, jun = 6, jul = 7, aug = 8, sep = 9, oct = 10, nov = 11, dec = 12 }; enum months jan =1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec }; 34
예제 7-7 : 열거형 -- TV 채널 #include <stdio.h> 공용체 (2/2) int main(void) enum TV afn = 2, sbs = 6, kbs2 = 7, kbs1 = 9, mbc = 11, ebs = 13, ytn = 60, mbn = 61, cnn = 51 }; printf(" 즐겨보는 TV 채널목록... \n\n"); printf(" AFN : %3d\n", afn ); printf(" SBS : %3d\n", sbs ); printf(" KBS2: %3d\n", kbs2 ); printf(" KBS1: %3d\n", kbs1 ); printf(" MBC : %3d\n", mbc ); printf(" EBS : %3d\n", ebs ); printf(" YTN : %3d\n", ytn ); printf(" MBN : %3d\n", mbn ); printf(" CNN : %3d\n", cnn ); } return 0; 35
참고문헌 [1] 서두옥, 이동호 ( 감수 ), ( 열혈강의 ) 또하나의 C : 프로그래밍은셀프입니다, 프리렉, 2012. [2] Paul Deitel, Harvey Deitel, "C How to Program", Global Edition, 8/E, Pearson, 2016. [3] SAMUEL P. HARBISON Ⅲ, GUY L. STEELE, C 프로그래밍언어, C : A Reference Manual, 5/E, Pearson Education Korea, 2005. [4] 문병로, 쉽게배우는알고리즘 - 관계중심의사고법, 개정판, 한빛아카데미, 2018. [5] 주우석, CㆍC++ 로배우는자료구조론, 한빛아카데미, 2015. [6] Behrouz A. Forouzan, Richard F. Gilberg, 김진외 7인공역, 구조적프로그래밍기법을위한 C, 도서출판인터비젼, 2004. [7] Brian W. Kernighan, Dennis M. Ritchie, 김석환외 2인공역, The C Programming Language, 2/E, 대영사, 2004. [8] 김일광, C 프로그래밍입문 : 프로그래밍을모국어처럼유창하게, 한빛미디어, 2004. [9] 정재은, " 다시체계적으로배우는 C 언어포인터 ", 정보문화사, 2003. 이강의자료는저작권법에따라보호받는저작물이므로무단전제와무단복제를금지하며, 내용의전부또는일부를이용하려면반드시저작권자의서면동의를받아야합니다. Copyright Clickseo.com. All rights reserved. 36