200 likes | 303 Views
구조체 (structure). 구조체의 선언 , 참조 구조체 포인터 개념 구조체 배열 함수의 매개변수로서 구조체 공용체 형 정의 (typedef) 예제 프로그램 ( 전자수첩 ). 구조체 (struct) 형이란 ? 다른 형의 여러 자료를 묶어 단일 명칭化 long 학번 , char 이름 [], float 평점 student 구조체형 (template) 선언 struct student { long st_id; /* 학번* / char name[20]; /* 이름* /
E N D
구조체(structure) 구조체의 선언, 참조 구조체 포인터 개념 구조체 배열 함수의 매개변수로서 구조체 공용체 형 정의(typedef) 예제 프로그램 (전자수첩)
구조체(struct)형이란? 다른 형의 여러 자료를 묶어 단일 명칭化 long 학번, char 이름[], float 평점 student 구조체형 (template) 선언 struct student { long st_id; /*학번*/ char name[20]; /*이름*/ float GPA; /*평점*/ }; 구조체의 선언
구조체의 선언(계속) • Student를 태그(tag), st_id, name, GPA를 멤버라고 함. • 의미: student라는 빵틀이 하나 정의되었다. • 구조체(struct) 변수 선언 • 일반형: int age; • 구조체: struct student x; student 형 x 학번 이름 평점
구조체의 선언(계속) • 구조체(struct) 변수의 배열 선언 • 일반형: int age[10]; • 구조체: struct student x[5]; student 형 x[0] x[1] x[2] x[3] x[4]
구조체의 선언(계속) • 구조체형(template)선언과 변수 선언 동시 struct student { long st_id; /*학번*/ char name[20]; /*이름*/ float GPA; /*평점*/ } x, class_a[5];
구조체의 참조 • 구조체(struct)의 멤버에 접근할려면? • 구조체 변수 이름.멤버이름 • x.st_id • 구조체(struct)의 멤버에 값 넣기 • x.st_id = 962300;
구조체내에 구조체 선언 • 구조체(struct)내에 멤버로 구조체 가능? • 멤버로 모든 형이 선언 가능 • school 구조체 struct school { char s_name[20]; /*이름*/ int s_number; /*번호*/ struct student s_hu; }; /*학생*/ 멤버 참조: school.s_hu.GPA
구조체의 초기화 • 정적,외부 변수인 경우 • 자동으로 null, 0 이 들어 감 • auto인 경우 쓰레기 값이 들어 감 • 예 struct school { char *s_name; /*이름*/ int s_number; /*번호*/ } a = {“kim”, 931111};
today ptr Month date year 100 100 포인터와 구조체 • 구조형을 포인터 변수로 선언 *** • 예 struct date { int month; int day; int year; } *ptr, today; (*ptr).month = 12; ptr->month = 12;
ptr 7 9 1998 100 100 포인터와 구조체(계속) • 포인터로 내용 참조 struct date { int month; int day; int year; } today = {7, 9, 1998}; ptr = &today; ptr->year 1998; ptr->month 7; ptr->day 9; [예 12.5] today
구조체 배열 • 포인터로 내용 참조 struct date { int month; int day; int year; } today[2] = {{7, 9, 1998}, {6, 5, 2002}}; 7 9 1998 today[0] 6 5 2002 today[1]
today[0] ptr 7 9 1998 100 100 6 5 2002 106 today[1] 구조체 포인터 • 포인터로 내용 참조 struct date { int month; int day; int year; } today[2], *ptr; ++ptr ?
10 20 30 \0 구조체 Linked List • Linked List 만들기 (크기 불예측시) struct node { int data; struct node *next; } *start; start = malloc(sizeof(struct node)); start->data = 10 start->next->data = 20; start
함수와 구조체 • 구조체의 멤버를 함수의 인수로 전달 struct date { int month; int day; int year; } today; …… func(today.day); • 구조체 전체를 주소로 전달 …… func(&today); func(y) struct date *y; { ……… }
함수와 구조체(계속) • 구조체 배열을 함수의 인수로 전달 struct date { int month; int day; int year; } today[10]; …… func(today); func(y) struct date *y; { y++ /* 다음 구조체 */ }
C i f 제일 큰 장소 1개 C i f 공용체(union) • 구조체와 유사, 멤버 중 1개만 유효 union template { struct template { char c; char c; int i; int i; float f; float f; } data; } data; ……
형 정의(typedef) • 임의의 자료형 정의 typedef float real; typedef int twobyte; struct student { char name[10]; float GPA; }; typedef struct student school; main() { real a; twobyte b; school c;
예제 프로그램 • 전자 수첩 이름 학과 주소 연락처 ===================================== 손기철 상업교육 신관동 50-3322 박주병 상업교육 웅진동 30-1122 채명수 수학교육 공산성 60-8949 …… *** 선택 메뉴 *** 1. 주소 입력 2. 검색 3. 주소록 출력 4. 끝
이름 학과 주소 연락처 이름 학과 주소 연락처 이름 학과 주소 연락처 전자 수첩 구조 리스트를 순회하면서 검색, 첨가
SUMMARY • 구조체의 선언, 초기화, 참조 • 구조체 포인터 를 증가하면 • 구조체 배열 • 구조체를 함수로 인수로 넘기는 3가지 • 공용체와 구조체의 차이? • 형 정의(typedef) • 예제 프로그램 (전자수첩)