Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 주차 : Structures and Unions. 2 Introduction structure 는 다른 타입들의 변수를 모은 타입이며 그 타입들을 structure 의 멤버 라 한다. union 은 다른 타입의 여러 변수들을 하나 의 기억 장소에 보관함으로써 한.

Similar presentations


Presentation on theme: "11 주차 : Structures and Unions. 2 Introduction structure 는 다른 타입들의 변수를 모은 타입이며 그 타입들을 structure 의 멤버 라 한다. union 은 다른 타입의 여러 변수들을 하나 의 기억 장소에 보관함으로써 한."— Presentation transcript:

1 11 주차 : Structures and Unions

2 2 Introduction structure 는 다른 타입들의 변수를 모은 타입이며 그 타입들을 structure 의 멤버 라 한다. union 은 다른 타입의 여러 변수들을 하나 의 기억 장소에 보관함으로써 한 객체가 여러 가지 타입으로 사용될 때 기억 장소 의 절약을 가져올 수 있다.

3 3 1. Structures 다른 타입의 여러 변수들을 모은 것 struct structure type 임을 나타내는 키워드 card tag_name struct card { int pips; char suit; };

4 4 pips, suit struct card type 의 멤버들 struct card c1, c2; struct card type 의 변수 c1, c2 의 선언 struct card{ int pips; char suit; } c1, c2;

5 5 c1.pips = 3; c2.suit = ‘ s ’ ; struct card type 의 변수 c1 의 멤버 pips 와 suit 를 각각 3 과 ‘ s ’ 로 초기화. 멤버 참조 연산자 c2 = c1; 변수 c2 의 모든 멤버에 c1 의 대응하는 멤버 의 값을 대입

6 6 typedef struct card card; struct card type 과 같은 card 라는 타입을 선언 card c3, c4, c5; card type 의 변수 3 개를 선언 typedef struct card{ int pips; char suit; } card; card c3, c4, c5;

7 7 struct fruit{ char *name; int calories; }; struct vegetable{ char *name; int calories; }; struct card{ int pips; char suit; } deck[52];

8 8 struct { int day; char day_name[4]; char month_name[4]; } yesterday, today, tomorrow; struct date { int day, month, year; char day_name[4]; char month_name[4]; }; struct date yesterday, today, tomorrow; typedef struct{ float re; float im; } complex; complex a, b, c[100];

9 9 2. Accessing Members of a Structure structure member operator ‘. ’ #define CLASS_SIZE 100 struct student{ char *last_name; int student_id; char grade; };... int main(void) { struct student tmp, class[CLASS_SIZE];

10 10 tmp.grade = ‘A’; tmp.last_name = “Casanova”; tmp.student_id = 910017;... /* Count the failing grades */ int fail(struct student class[])/* is same as *class */ { int i, cnt = 0; for(i = 0; i < CLASS_SIZE; i++){ cnt += class[i].grade == ‘F’; return cnt; }

11 11 structure 에 대한 pointer 를 경유해서 멤버를 참 조 struct complex{ double re; double im; }; typedef struct complex complex; void add(complex *a, complex *b, complex *c) { a->re = b->re + c->re; a->im = b->im + c->im; }

12 12 a->re 는 (*a).re 와 같다.

13 13 3. Using Structures with Functions structure type 도 다른 type 과 마찬가지로 함수의 인자가 될 때는 call-by-value 로 전달된다. 단, structure 의 크기가 클 때는 인자를 복 사하는데 시간이 많이 걸리고 공간상으 로도 낭비이므로 주로 structure 의 포인 터를 인자로 주는 방법을 사용한다.

14 14 4. Initializing of Structures structure type 변수의 초기화는 배열의 초기화와 흡사하다. card c = {13, ‘h’}; complex a[3][3] = { {{1.0, -0.1}, {2.0, 0.2}, {3.0, 0.3}}, {{4.0, -0.4}, {5.0, 0.5}, {6.0, 0.6}},}; /* 배열의 나머지 원소는 0 으로 초기화 */ struct fruit frt = {“plum”, 150);

15 15 5. Unions 멤버중 하나만을 취한다. union union type 임을 나타내는 키워드 int_or_float tag_name union int_or_float{ int i; float f; };

16 16 int i, float f union int_or_float type 의 멤버들 union int_or_float a, b, c union int_or_float type 의 변수 a, b, c 의 선언 union int_or_float type 의 크기는 int i, float f 중 큰 것의 크기다. 변수 a, b, c 는 참조될 때 int i 나 float f 중 어느 하나로만 참조될 수 있다.

17 17 typedef union int_or_float{ int i; float f; } number; int main(void) { number n; n.i = 4444;/* int i 로 참조 */ printf(“i: %10d f: %16.10e\n”, n.i, n.f); n.f = 4444.0;/* float f 로 참조 */ printf(“i: %10d f: %16.10e\n”, n.i, n.f); return 0; }

18 18

19 19 과제 4 (~5.31) 데이터 파일 (hw3.dat) 에서 알파벳 문자 를 읽어서 알파벳순으로 ( 화면에 ) 출력하 는 프로그램을 작성하라. 강의 자료를 참고하여 작성. hw3.dat 는 강의 홈페이지 BBS 에 있음. 5 월 16 일 현재 번호로 136 번 글. 배열을 알파벳순으로 정렬 (sorting) 하는 함 수를 작성하라. 보고서 제출양식 지킬 것.


Download ppt "11 주차 : Structures and Unions. 2 Introduction structure 는 다른 타입들의 변수를 모은 타입이며 그 타입들을 structure 의 멤버 라 한다. union 은 다른 타입의 여러 변수들을 하나 의 기억 장소에 보관함으로써 한."

Similar presentations


Ads by Google