170 likes | 331 Views
structure making your own data type. C 에서 제공하는 data type int, char, double, ... 같은 종류의 data type 으로 이루어진 복수의 변수가 필요할 경우 배열의 사용 int a[100]; char s[50]; 다른 종류의 data type 으로 이루어진 복수의 변수가 필요할 경우 ? 서점의 책관리 프로그램 책 한권에 필요한 데이터 책 이름 , 저자 이름 , 가격
E N D
structure making your own data type • C에서 제공하는 data type • int, char, double, ... • 같은 종류의 data type으로 이루어진 복수의 변수가 필요할 경우 • 배열의 사용 int a[100]; char s[50]; • 다른 종류의 data type으로 이루어진 복수의 변수가 필요할 경우? • 서점의 책관리 프로그램 • 책 한권에 필요한 데이터 • 책 이름, 저자 이름, 가격 • char title[50]; char author[50]; int price; • 책 100권을 관리하는 서점일 경우 • char title[100][50]; • char author[100][50]; • int price[50]; 첫번째 책의 제목 출력 printf(“%s\n”, __________);
structure를 사용하여 새로운 data type을 만듬 • 서점의 책을 위한 새로운 data type을 만듬 structure (구조체) 사용 #include <stdio.h> struct book { char title[50]; char author[50]; int price; }; main() { struct book a; ..... } 새로운 명령어: struct (구조체를 만듬) 1. 새로운 구조체의 이름: book 2. book이라는 이름은 마음대로 작명가능 3. 정의하는 위치 프로그램의 맨 위 a라는 변수를 만듬 (a의 data type struct book)
structure의 사용 • struct 변수의 사용 struct book { char title[50]; char author[50]; int price; }; main() { structbook a; a.price = 5000; strcpy( a.title, “C programming”); strcpy( a.author, “Suh”); printf(“%d\n”, a.price); printf(“%c\n”, a.title[0]); printf(“%s\n”, a.author); } struct book a, b, c; 여러개의 변수 선언
struct의 사용 복습 • 책 제목을 scanf를 사용해 keyboard에서 읽어 들임 • struct 변수의 초기화 struct book { char title[50]; char author[50]; int price; } main() { structbook a; scanf(“_______”, _____________); } 초기화 main() { struct book a = { “C programming”, “Suh”, 5000 }; } main() { struct book a; a = { “C programming”, “Suh”, 5000 }; } No way!
struct의 편리함 • struct의 copy main() { struct book a = { “C programming”, “Suh”, 5000 }; struct book b; b = a; printf(“%d\n”, b.price); } structure를 copy함
structure의 array • 책 5권을 관리하는 프로그램 main() { struct book a[5]; int i; for ( i = 0 ; i < 5 ; ++i ) a[i].price = 5000; for ( i = 0 ; i < 5 ; ++i ) scanf(“%s”, ______________); } 책 5권의 price를 가격이 전부 5000원 책 5권의 title을 읽어 들임
structure의 array의 초기화 • structure의 array를 초기화 하는 방법 main() { struct book a[5] = { “bible”, “god”, 2000, “ddd”,”kim”, 3000, “ccc”, “lee”, 4000, “ffff”, “park”, 5000, “gggg”, “choi”, 3000}; printf(“%d”, a[0].price); printf(“%c”, a[0].title[0]); printf(“%c”,a[0].title[1]); printf(“%s”, _________); 첫번째 책의 제목 a[1] = a[0]; printf(“%d”,a[1].price); 초기화
Nested Structure • structure로 일단 선언하면, int, char와 같은 data type처럼 취급됨 • 다른 structure를 선언하는데 사용할 수 있음 struct fullname { char first[30]; char last[30]; }; main() { struct person a; a.age = 50; strcpy( a.name.first, “John”); strcpy( a.name.last, “Doyle”); } struct person { int age; struct fullname name; }; a.name.last
Pointers to Structures • pointer to int의 복습 • pointer to structure의 복습 int a = 3; int *b; b = &a; 100번지 – 103번지 3 a의 값 ___, &a의 값 _____ b의 값 ______, *b의 값 _____ struct book { char title[50]; int price; }; main() { struct book a = {“aaa”, 500}; struct book *b; b = &a; } 100번지 – 150번지 “aaa” 150번지 – 153번지 500 &a의 값 b의 값
Pointers to Structures • structure에 대한 pointer를 사용할 수 있다. • 약속 • 배열의 경우 struct book a = {“aaa”, 500}; struct book *b; b = &a; a.price의 값 (*b).price의 값 (*b).price == b->price struct book a[2] = { “aaa”, 500, “bbb”, 1000 }; struct book *b; b = &a[0]; b->price의 값은? b = &a[1]; b->price의 값은?
Pointer to Structure의 사용 • 함수의 인수로서 pointer to structure struct book a = {“aaa”, 500}; print_book( &a ); void print_book( struct book *b ) { printf(“%s\n”, b->title); printf(“%d\n”, b->book); } struct book a[2] = { “aaa”, 500, “bbb”, 1000}; print_book( &a[0] ); print_book( &a[1] ); 같음 void print_book( struct book *b ) { printf(“%s\n”, (*b).title); printf(“%d\n”, (*b).book); }
함수의 인수로서 pointer to structure의 사용 • structure 배열의 주소를 함수의 인수로서 넘겨주는 경우 struct book a[2] = { “aaa”, 500, “bbb”, 1000}; print_book1( &a[0] , 2 ); void print_book1( struct book *b, int N) { int i; for ( i = 0 ; i < N ; ++i ) /*가격만 출력*/ printf(“%d\n”, b[0].price); } void print_book1( struct book *b, int N) { int i; for ( i = 0 ; i < N ; ++i ) { printf(“%d\n”, b->price); ++b; } }
structure의 값을 함수에 넘겨주는 3가지 방법 • 책 값의 10%가 세금이라고 하고, 세금을 계산하는 함수 • 책값만 넘겨줌 • structure 전체를 넘겨줌 • structure의 주소를 넘겨줌 세금의 액수 (500 * 0.1 = 50원)를 return하는 함수 compute_tax를 구함 struct book { char title[50]; int price; }; struct book a = {“aaa”, 500}; 책값: a.price (int) structure 전체: a (struct book) structure의 주소: &a (struct book *)
3가지 방법 • 책값만 넘겨줌 • structure 전체를 넘겨줌 • pointer to structure 를 넘겨줌 struct book a = {“aaa”, 500}; double b; b = compute_tax( a.price ); double compute_tax( __________ c ) { return( c * 0.1 ); } struct book a = {“aaa”, 500}; double b; b = compute_tax( a ); double compute_tax( __________ c ) { return( ________________ ); } struct book a = {“aaa”, 500}; double b; b = compute_tax( &a ); double compute_tax( __________ c ) { return( ________________ ); }
3가지 방법 중 어떤 방법이 좋은가? • 일반적으로는 structure의 주소를 넘겨주는 방법을 가장 많이 사용 • structure를 넘겨주는 방법: 비효율적임 (원소를 전부 copy) • 예외: return value로서의 structure • 복소수를 나타내는 structure complex의 정의 struct complex { double real; double imag; }; main() { struct complex a = { 3.0, 4.0 }, b = { 4.5, 3.7}, c; c = add( a, b); } ________________ add( _______________a, _______________b);
struct complex add함수의 작성 • struct complex함수의 작성 struct complex add(struct complex a, struct complex b) { struct complex c; c.real = a.real + b.real; c.imag = a.imag + c.imag; return ( c ); }
연습문제 • 다음 프로그램을 실행했을 때 화면에 나타나는 것은? struct house float sqft; int rooms; int stories; char address[40]; }; main() { struct house fruzt = {1560.0, 6,1,”22 Spiffo Road”}; struct house *sign; sign = &fruzt; printf(“%d %d\n”, fruzt.rooms, sign->stories); printf(“%s\n”,fruzt.address); printf(“%c %c\n”,sign->address[3],fruzt.address[4]); }