프로그래밍 1 1 Chapter 9. Structures May, 2016 Dept. of software Dankook University http://embedded.dankook.ac.kr/~baeksj
구조체의개념 (1/4) 2 (0,0) 구조체 : 다양한종류의데이터로구성된사용자정의데이터타입 복잡한자료를다루는것을편하게해줌 예 #1: 정수로이루어진 x, y 의좌표 (4,3) struct point int x; int y; 예 #2: 개인정보구조체 struct person char name[4]; int age; int height; ; struct person i1, i2; i1 0xfffffffc 0x18 0x14 0x13 0x12 0x11 0x10 0x1 memory height age name[3] name[2] name[1] name[0] struct person 32bits
구조체의개념 (2/4) 3 구조체의형식 구조체멤버의접근 struct struct_name variable 1; variable 2;... ; 예제 두점의기울기구하기 structure tag structure member struct point int x; int y; struct point int x; int y; ; int main(void) double gradient; struct point pt1 = 0, 0; struct point pt2; pt2.x = 4; pt2.y = 3; printf("point 1 (%d, %d)\n", pt1.x, pt1.y); printf("point 2 (%d, %d)\n", pt2.x, pt2.y); gradient = (double)(pt2.y pt1.y)/(pt2.x pt1.x); printf("gradient = %lf\n", gradient);
구조체의개념 (3/4) 4 구조체의메모리구조 memory struct point int x; int y; ; int main() double gradient; struct point pt1 = 0, 0; struct point pt2; struct pt2 struct pt1 3 4 0 0 pt2.x = 4; pt2.y = 3;... 32bits
구조체의개념 (4/4) 5 구조체의사용예 학생구조체선언및구조체변수초기화 #include <string.h> struct student int id; char name[24]; char addr[128]; int zip; ; int main() struct student stu1; struct student stu2 = 20151234, "Taylor Swift", "Hollywood Blvd, USA", 92013; printf("student #2\n"); printf(" ID : %d\n", stu2.id); printf(" Name : %s\n", stu2.name); printf(" Addr : %s\n", stu2.addr); printf(" ZIP : %d\n", stu2.zip); stu1.id = 20135678; strcpy(stu1.name, "Sam Smith"); strcpy(stu1.addr, "Baker st. 207, UK"); stu1.zip = 20002; printf("student #2\n"); printf(" ID : %d\n", stu1.id); printf(" Name : %s\n", stu1.name); printf(" Addr : %s\n", stu1.addr); printf(" ZIP : %d\n", stu1.zip);
구조체의배열 6 구조체연습문제 학생구조체를이용하여 5 명의학생데이터를저장하려면? Hint: 배열사용 #include <string.h> struct student int id; char name[24]; char addr[128]; int zip; ; struct student arr[5]; struct stu1 struct stu2 memory int main() arr[0].id = 123456; strcpy(arr[0].name, "Taylor Swift"); strcpy(arr[0].addr, "Hollywood Blvd, USA"); arr[0].zip = 92013;... printf("student #0\n"); printf(" ID : %d\n", arr[0].id); printf(" Name : %s\n", arr[0].name); printf(" Addr : %s\n", arr[0].addr); printf(" ZIP : %d\n", arr[0].zip); 32bits
구조체의배열 7 구조체배열선언및초기화 struct student int id; char name[24]; char addr[128]; int zip; ; struct student arr[5]; int main() arr[0].id = 123456; strcpy(arr[0].name, "Taylor Swift"); strcpy(arr[0].addr, "Hollywood Blvd, USA"); arr[0].zip = 92013; struct student int id; char name[24]; char addr[128]; int zip; arr[ ] = 123456, Taylor Swift, Hollywood Blvd, USA, 92013, ; struct student int id; char name[24]; char addr[128]; int zip; arr[5]; int main() arr[0].id = 123456; strcpy(arr[0].name, "Taylor Swift"); strcpy(arr[0].addr, "Hollywood Blvd, USA"); arr[0].zip = 92013; struct yourstudent int id; char name[24]; char addr[128]; int zip; yourarr[] = 123456, "Taylor Swift", "Hollywood Blvd, USA", 92013, 654321, "Sam Smith", "Baker st. 207, UK", 20002, 987654, "John Doe", "Unknown", ;
구조체의중첩 8 구조체의중첩 기존의구조체를활용한새로운구조체정의 struct point int x; int y; ; struct rect struct point pt1; struct point pt2; ; int main(void) struct rect myscreen; int area; myscreen.pt1.x = 3; myscreen.pt1.y = 6; myscreen.pt2.x = 9; myscreen.pt2.y = 12; (3,6) (9,12) area = (myscreen.pt2.x myscreen.pt1.x) * (myscreen.pt2.y myscreen.pt1.y); printf("area = %d\n", area);
구조체와함수 (1/4) 9 함수에서구조체를사용하는방법 1) 함수에독립적인인자로전달 2) 구조체를값으로전달 3) 구조체를포인터로전달 int sum(int x, int y) return x + y; 1) 인자전달 2) 값으로전달 3) 포인터로전달 struct point sum(int x, int y) struct point temp; temp.x = x; temp.y = y; return temp;
구조체와함수 (2/4) 10 1) 함수에독립적인인자로전달 struct point int x; int y; ; void Print_Point(int x, int y) struct point ptf; ptf.x = x; ptf.y = y; printf("x, y = (%d, %d)\n", ptf.x, ptf.y); int main() struct point ptm = 3, 5; Print_Point(ptm.x, ptm.y); struct point ptm int y int x struct point ptf memory 5 3 5 3 5 3 32bits
구조체와함수 (3/4) 11 2) 구조체를값으로전달 Call by value memory struct point int x; int y; ; struct point ptm 5 3 void Print_Point(struct point ptf) printf("x, y = (%d, %d)\n", ptf.x, ptf.y); int main() struct point ptm = 3, 5; Print_Point(ptm); struct point ptf 5 3 * Print_Point() 를수정하여 Get_Point() 로만들고리턴값을구조체로만들어보세요. - 좌표이동 x, y 값에 5 를더한다. 32bits
구조체와함수 (4/4) 12 3) 구조체를포인터로전달 Call by reference memory struct point int x; int y; ; struct point *ptf 100 void Print_Point(struct point *ptf) printf("x, y = (%d, %d)\n", (*ptf).x, (*ptf).y); (*ptf).x += 1; (*ptf).y += 1; int main() struct point ptm = 3, 5; stuct point ptm 5 3 100 Print_Point(&ptm); printf("x, y = (%d, %d)\n", ptm.x, ptm.y); *. 또는 -> 차이점 32bits
구조체와포인터 13 포인터를통한구조체접근 #include <string.h> struct person char name[20]; int age; int height; ; #define NR_TEAM 5 int main(void) struct person team[nr_team]; struct person *pptr; pptr = team; int i; for(i=0; i<nr_team; i++) printf("enter Nr=%d team member info(name age height): ", i); scanf("%s %d %d", (pptr+i) >name, &(pptr+i) >age, &(pptr+i) >height); for(i=0; i<nr_team; i++) printf("%s:%d:%d\n", (pptr+i) >name, (pptr+i) >age, (pptr+i) >height);
구조체 typedef 14 typedef 를통한자료구조정의 #include <string.h> typedef int MYINT; typedef char MYCHAR; typedef struct person MYCHAR name[20]; MYINT age; MYINT height; PERSON; int main(void) PERSON i1; strcpy(i1.name, "Testname"); i1.age = 20; i1.height = 170; printf("%s:%d:%d\n", i1.name, i1.age, i1.height); struct person i2 = "Test2name", 30, 180; printf("%s:%d:%d\n", i2.name, i2.age, i2.height);
구조체의동적할당 15 malloc 과구조체 pointer 활용한구조체의동적할당 #include <string.h> #include <stdlib.h> #define MAXNAME 18 typedef struct person char name[maxname]; int age; int height; PERSON; 26? int main(void) PERSON *new; printf("sizeof struct person=%d\n", sizeof(person)); new = (PERSON *)malloc(sizeof(person)); printf("enter team member info(name age height): "); scanf("%s %d %d", (new) >name, &(new) >age, &(new) >height); printf("%s:%d:%d\n", new >name, new >age, new >height);
회원관리프로그램 (1/3) 16 Using double linked list #include <string.h> #include <stdlib.h> #define MAXNAME 20 typedef struct person char name[maxname]; int age; int height; struct person *prev; struct person *next; PERSON; PERSON * head; PERSON * GetNewNode(void) PERSON * new = (PERSON *)malloc(sizeof(person)); new >prev = NULL; new >next = NULL; return new;
회원관리프로그램 (2/3) 17 Using double linked list void InsertAtHead(PERSON * new) if(head == NULL) head = new; return; head >prev = new; new >next = head; head = new; void InsertAtTail(PERSON * new) PERSON * temp = head; if(head == NULL) head = new; return; while(temp >next!= NULL) temp = temp >next; temp >next = new; new >prev = temp;
회원관리프로그램 (3/3) 18 Using double linked list void Print() PERSON * curr= head; printf("forward search\n"); while(curr!= NULL) printf("%s:%d:%d\n", curr >name, curr >age, curr >height); curr = curr >next; int main(void) PERSON * curr; int i; head = NULL; for (i=0; i<10; i++) curr = GetNewNode(); printf("enter Nr=%d team member info(name age height): ", i); scanf("%s %d %d", (curr) >name, &(curr) >age, &(curr) >height); InsertAtHead(curr); Print();
과제 19 이름, 나이, 키를저장할수있는회원관리프로그램작성 Keyword: structure, dynamic allocation, double linked list 메모리가허락하는한무한대의회원추가기능 회원추가시이름순 ( 오름차순 ) 정렬하여 insert 회원삭제기능회원 print 기능이름으로검색기능 Help 기능 Bonus: 이름 / 나이 / 키로정렬기능 ( 동일한 key 값인경우다른 key 를기준으로하여정렬 )
union 20 Union: 다른종류의데이터를하나의저장공간에서다룰수있도록지원 typedef struct sbox int mem1; int mem2; double mem3; SBox; vs. typedef union sbox int mem1; int mem2; double mem3; SBox; int int vs. double double
union 21 union 키워드사용 typedef struct dbshort unsigned short upper; unsigned short lower; DBShort; typedef union rdbuf int upper lower int ibuf; char bbuf[4]; dbuf[0] dbuf[1] dbuf[2] dbuf[3] DBShort sbuf; RDBuf; int main(void) RDBuf buf; printf("enter an Interger: "); scanf("%d", &(buf.ibuf)); printf("upper 2B: %u\n", buf.sbuf.upper); printf("lower 2B: %u\n", buf.sbuf.lower); printf("msb [0] : %d\n", buf.bbuf[0]); printf("msb [1] : %d\n", buf.bbuf[1]); printf("msb [2] : %d\n", buf.bbuf[2]); printf("lsb [3] : %d\n", buf.bbuf[3]);
enum 22 Enum: unique types with values ranging over a set of named constants called enumerators enum month JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC; //enum month JAN=1,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC; //enum month JAN=10,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC; //enum month JAN=13,FEB,MAR,APR,MAY,JUN,JUL=83,AUG,SEP,OCT,NOV,DEC; int main(void) int i; printf("%d\n", MAR); for(i=jan;i<=dec;i++) printf("%d ",i);
이장의결론 23 구조체의개념을이해구조체와함수구조체의배열과포인터구조체의동적할당과리스트 Typedef, enum, union