2 8 1 typedef 8 1 typedef 1

  • Slides: 52
Download presentation

2 8. 1 형 정의 typedef • (예 8. 1) typedef 정의와 선언 /*

2 8. 1 형 정의 typedef • (예 8. 1) typedef 정의와 선언 /* (1) 정의 */ typedef int BOOL; typedef unsigned char TEXT; typedef int INCHES, FEET, YARDS; typedef int vector[10]; typedef char *string; /* (2) 선언 */ BOOL flag; TEXT buf[100]; INCHES length, width; vector a, b; string text, input; /* /* /* int flag; 와 같은 의미 */ unsigned char buf[100]; 와 같은 의미 */ int length, width; 와 같은 의미 */ int a[10], b[10]; 과 같은 의미 */ char *text, *input; 과 같은 의미 */ <typedef와 #define의 차이점> - typedef는 자료형에 대해서만 기호 이름을 부여 할 수 있다. - typedef는 전처리기가 아닌 컴파일러에 의해 실행된다. (예 8. 1) typedef

3 8. 1 형 정의 typedef • (예 8. 2) typedef의 사용(1) /* (1)

3 8. 1 형 정의 typedef • (예 8. 2) typedef의 사용(1) /* (1) 정의 */ #define N 3 /* 벡터(vector)와 행렬(matrix)의 크기 */ typedef double scalar; typedef scalar vector[N]; typedef scalar matrix[N][N]; void add(a, b, c) vector a, b, c; /* double a[3], b[3], c[3]; 과 같은 의미 */ { int i; for (i = 0; i < N; i++) a[i] = b[i] + c[i]; } - 함수 add()는 벡터를 연산하는 함수이다. - matrix의 선언은 기 정의된 vector를 사용하면 다음과 같이도 할 수 있다. typedef vector matrix[N]; (예 8. 2) typedef의 사용

4 8. 1 형 정의 typedef • (예 8. 3) typedef의 사용(2) scalar dot_product(a,

4 8. 1 형 정의 typedef • (예 8. 3) typedef의 사용(2) scalar dot_product(a, b) /* 벡터 a와 벡터 b의 내적(dot product) */ vector a, b; { int i; scalar s = 0; for (i = 0; i < N; i++) s += a[i] * b[i]; return s; } void multiply(a, b, c) /* 행렬 곱셈: a = b*c */ matrix a, b, c; { int i, j, k; for (i = 0; i < N; i++) for (j = 0; j < N; j++) for (c[i][j] = 0, k = 0; k < N; k++) c[i][j] += a[i][k] * b[k][j]; } - 벡터의 내적 구하는 dot_product와 두 행렬의 곱을 계산하는 multiply - scalar, vector의 정의는 예8. 2에서 정의 - matrix a, b, c; 는 double a[N][N], b[N][N], c[N][N]; 과 동일. (예 8. 3) typedef의 사용

15 8. 2 구조형 struct • (예 8. 10 계속) 구조형의 초기화 – (2)는

15 8. 2 구조형 struct • (예 8. 10 계속) 구조형의 초기화 – (2)는 다음과 동일하다. /* (2)’ */ static struct month { char *mname; int day; } month_tab[] = { {“January”, 31}, {“March”, 31}, {“May”, 31}, {“July”, 31}, {“September”, 30}, {“November”, 30}, (예 8. 10 계속) 구조형의 초기화 {“February”, {“April”, {“June”, {“August”, {“October”, {“December”, 28}, 30}, 31}, 31} };

18 8. 2 구조형 struct – (예 8. 12) 간접 멤버 연산자 -> 의

18 8. 2 구조형 struct – (예 8. 12) 간접 멤버 연산자 -> 의 사용 예 • 구조형 포인터 변수 p에 구조형 변수 temp의 포인터를 배 정하여 사용하고 있다. • p -> grade는 p가 가리키는 구조형의 멤버 grade를 뜻함. /* (예 8. 12) */ struct student temp, *p = &temp; temp. grade = ‘A’; temp. student_name = “Chanho Park”; temp. student_id = 90118; 수식 연산 우선 순위 결과 temp. student_name temp. grade temp. student_id (*p) -> student_name+2 *p -> student_name+2 *(p -> student_name+2) p -> student_name p -> grade p -> student_id *((*p) -> student_name) + 2 *(p -> student_name) + 2 p -> student_name [2] Chanho Park A 90118 /* 에러 */ E a (예 8. 12) -> 의 사용

23 8. 3 구조형과 함수 • 포인터 기반 함수와 구조형 기반 함수 – 다음

23 8. 3 구조형과 함수 • 포인터 기반 함수와 구조형 기반 함수 – 다음 구조형을 정의하여 사용하자. 1 2 3 4 5 6 /* File : complex. h For prog 8 -16. c & prog 8 -17. c */ typedef struct { float re; float im; } complex; 헤더 file complex. h

25 8. 3 구조형과 함수 • (예 8. 16 계속) 17 18 19 20

25 8. 3 구조형과 함수 • (예 8. 16 계속) 17 18 19 20 void prnt(complex *cp) { printf(“%. 3 f + %. 3 f in”, cp->re, cp->im); } 21 22 23 24 25 26 27 28 int main(void) { complex c 1, c 2, c 3; assign(&c 1, 1. 0, 2. 0); assign(&c 2, 3. 0, 4. 0); add(&c 1, &c 2, &c 3); prnt(&c 3); } (예 8. 16 계속) output 4. 000 + 6. 000 i

29 8. 4 공용형 union • (예 8. 20) 구조형과 공용형의 혼용 – union

29 8. 4 공용형 union • (예 8. 20) 구조형과 공용형의 혼용 – union fromwhere은 foreign_traveller와 korean_traveller 라는 멤버를 가진다. – 이 두 멤버는 동시에 사용되지 않으므로 공용형 멤버로 정의되었다. /* (예 8. 20) */ struct foreign_traveller { int country_code; int city_code; *pc; char passport_id[20]; }; struct korean_traveller { int district_code; char resident_id[13]; }; union fromwhere { struct foreign_traveller f_trav; struct korean_traveller k_trav; }; (예 8. 20) struct와 union의 혼용

30 8. 4 공용형 union • (예 8. 20 계속) 구조형과 공용형의 혼용 –

30 8. 4 공용형 union • (예 8. 20 계속) 구조형과 공용형의 혼용 – traveller_record란 구조형의 한 멤버로 union fromwhere 형을 사용하고 있다. /* (예 8. 20 계속) */ typedef struct { int serial_no; char name[10]; short iskorean; union fromwhere a; } traveller_record; (예 8. 20 계속)

35 /* 예제 8. 2(계속) */ /* (4) */ struct employee student, faculty, staff;

35 /* 예제 8. 2(계속) */ /* (4) */ struct employee student, faculty, staff; 예제 8. 2(계속) /* (5) */ struct sample { char c; float *pf; struct sample *next; } x; /* (6) */ struct { unsigned icon : 8; unsigned color : 4; unsigned underline : 1; unsigned blink : 1; } screen[25][80]; - (4) 변수 student, faculty, staff를 struct employee형으로 선언 - (5) 구조형의 3째 멤버 next는 동일한 구조형 자료에 대한 포인터이다. - (6) screen이라는 이름의 구조형 배열로 원소는 2000개이다. 각 원소는 4개의 멤버로 구성되고, 각 멤버는 각각 길이가 8비트, 4비트, 1비트이다. [해 설]

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 /* File : ex 8 -4. c 복소수 계산 프로그램 */ #include <stdio. h> #define CMPLX struct complex CMPLX { double re; double im; }; int main(void) { static CMPLX za = {3. 0, 4. 0}; static CMPLX zb = {5. 0, 6. 0}; CMPLX z, cadd(), cmult(); void cprint(); z = cadd(za, zb); cprint(z); z = cmult(za, zb); cprint(z); } /* ------ 복소수 덧셈 ------ */ CMPLX cadd(CMPLX za, CMPLX zb) { CMPLX z; z. re = za. re + zb. re; z. im = za. im + zb. im; return z; } 37 예제 8. 4

25 26 27 28 29 30 31 32 33 34 35 36 37 /*

25 26 27 28 29 30 31 32 33 34 35 36 37 /* ------ 복소수 곱셈 ------ */ CMPLX cmult(CMPLX za, CMPLX zb) { CMPLX z; z. re = za. re * zb. re - za. im * zb. im; z. im = za. re * zb. im + za. im + zb. re; return z; } /* ------ 복소수 출력 ------ */ void cprint(CMPLX z) { printf(“%. 3 f + %. 3 f in”, z. re, z. im); } - 줄4로 CMPLX를 struct complex와 같게 선언함. - 복소수를 실수부 re(real)와 허수부 im(imaginary)로 구성된 구조형으로 정의. - 두 복소수의 합과 곱을 구하는 프로그램 8. 000 + 10. 000 i -9. 000 + 38. 000 i 38 예제 8. 4(계 속) [해 설] output

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /* File : ex 8 -5. c struct와 내포된 struct */ #include <stdio. h> int main(void) { static struct s 1 { char c[4], *s; } s 1 = {“abc”, “def” }; static struct s 2 { char *cp; struct s 1 ss 1; } s 2 = {“ghi”, { “jkl”, “mno” }}; printf(“s 1. c[0]=%ct”, s 1. c[0]); printf(“*s 1. s=%cn”, *s 1. s); printf(“s 1. c=%st”, s 1. c); printf(“s 1. s=%sn”, s 1. s); printf(“s 2. cp=%st”, s 2. cp); printf(“s 2. ss 1. s=%sn”, s 2. ss 1. s); printf(“++s 2. cp=%st”, ++s 2. cp); printf(“++s 2. ss 1. s=%sn”, ++s 2. ss 1. s); } - struct의 멤버로 struct를 사용할 수 있다. s 1. c[0]=a s 1. c=abc s 2. cp=ghi ++s 2. cp=hi *s 1. s=def s 2. ss 1. s=mno ++s 2. ss 1. s=no 39 예제 8. 5 [해 설] output

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 /* File : ex 8 -6. c struct의 포인터를 인수로 사용하는 함수 match */ #include <stdio. h> struct student { char name[20]; int id; } student = { “Kim”, 73 }; int main(void) { int id, match(); char *name 1 = “Lee”, *name 2 = “Kim”; if (id = match(&student, name 1)) printf(“%s’s ID is %d. n”, name 1, id); else printf(“He is not %s. n”, name 1); if (id = match(&student, name 2)) printf(“%s’s ID is %d. n”, name 2, id); else printf(“He is not %s. n”, name 2); } int match(struct student *r, char *n) { if (strcmp(r->name, n) == 0) return r->id; return 0; /* 실패 */ } 40 예제 8. 6 struct의 포인터를 인 수로 사용

1 2 3 4 5 6 7 /* File : ex 8 -8. c

1 2 3 4 5 6 7 /* File : ex 8 -8. c struct의 복사 함수 */ #include <stdio. h> struct st { int id; char name[10]; }; 8 9 10 11 12 13 14 15 16 17 18 19 void st_copy(struct st *s 1, struct st *s 2) { int j; s 1 -> id = s 2 -> id; for (j = 0; j < 10; j++) s 1 -> name[j] = s 2 -> name[j]; } void st_print(struct st *s) { printf(“번호 = %d, 이름 = %sn”, s -> id, s -> name); } 20 44 예제 8 -8

51 /* 예제 8. 13 다음 구조형 선언의 기억 형태는? */ struct { unsigned

51 /* 예제 8. 13 다음 구조형 선언의 기억 형태는? */ struct { unsigned icon: 8; unsigned color: 4; unsigned underline: 1; unsigned blink: 1; } screen[25][80]; - screen 의 2000개 원소 중 첫 번째 원소의 저장 모습이다. 14비트 크기 임. 예제 8. 13 [해 설]

1 /* File : ex 8 -14. c 2 비트 필드 조작 프로그램 */

1 /* File : ex 8 -14. c 2 비트 필드 조작 프로그램 */ 3 #include <stdio. h> 4 int main(void) 5 { 6 struct { 7 unsigned carry: 2; /* overflow */ 8 unsigned parity: 2; /* 1 = 짝수; 0 = 홀수 */ 9 unsigned acarry: 2; /* 보조 carry */ 10 unsigned zero: 1; /* 결과가 0 */ 11 unsigned sign: 1; /* 결과가 음수 */ 12 } f 8080; 13 f 8080. carry = f 8080. acarry = f 8080. sign = 0; 14 f 8080. parity = f 8080. zero = 1; 15 if (f 8080. carry == 0) printf(“carry 없음n”); 16 if (f 8080. parity == 0) printf(“짝수 parityn”); 17 if (f 8080. acarry == 0) printf(“보조 carry 없음n”); 18 if (f 8080. zero == 0) printf(“결과가 0이 아님n”); 19 if (f 8080. sign == 0) printf(“결과가 양수임n”); 20 } carry 없음 보조 carry 없음 결과가 양수임 52 예제 8 -14 output