struct 1 n struct struct humantag char name20
- Slides: 20
선언방식 struct 구조체태그 { 데이터형 필드 1; …. 데이터형 필드n; }; struct 구조체태그 구조체변수; struct human_tag { char name[20]; char id[13]; int age; }; struct human_tag lee, han, kim; struct 구조체태그 { 데이터형 필드 1; …. 데이터형 필드n; } 구조체변수; struct human_tag { char name[20]; char id[13]; int age; } lee, han, kim; struct { 데이터형 필드 1; …. 데이터형 필드n; } 구조체변수; struct { char name[20]; char id[13]; int age; } lee, han, kim;
선언예 선언 struct sungjuk { int kor; int eng; int tot; float ave; }; struct sungjuk hong, leem, lee; 메모리 모양 k e t . a k e t a …. . o n o v. . r g t e hong leem lee 사용 . 을 이용 구조체필드 의 형식 hong. kor = 90; leem. eng = 80; lee. tot = lee. kor + lee. eng;
구조체의 사용 struct sungjuk { int kor; int eng; int tot; float ave; }; struct sungjuk hong, leem, lee; 값 hong. kor hong. eng hong. tot 예> 출력 printf(“%d”, hong. eng); 주소 & hong. kor &hong. eng 예> scanf(“%d”, &hong. kor); 구조체 전체 대입 예> hong = leem; hong. kor = leem. kor; hong. eng = leem. eng; hong. tot = leem. tot; hong. ave = leem. ave;
구조체 이용한 성적 프로그램 #include <stdio. h> struct jumsu { char name[10]; int kor; int eng; int mat; int tot; float ave; }; struct jumsu lee; void mian(void) { printf(“이 름 : ”); printf(“국 어 : ”); printf(“영 어 : ”); printf(“수 학 : ”); 문자열 임 주소 지정 scanf(“%s”, lee. name); scanf(“%d”, &lee. kor); scanf(“%d”, &lee. eng); scanf(“%d”, &lee. mat);
구조체 선언 과 초기화, 복사 struct person { char name[10]; int age; char job[10]; }; void main(void) { struct person man = {“임꺽정”, 35, “산지기”}; struct kwail { char irum[20]; int price; char color[10]; }; void main(void) { struct kwail apple 1, apple 2; 페이지 278 strcpy(apple 1. irum, “사과”); apple 1. price = 1000; strcpy(apple 1. color, “RED”); apple 2 = apple 1; printf(“품명 : %sn”, apple 2. irum);
구조체의 크기 계산 struct kwail { char irum[20]; int price; char color[10]; }; void main(void) { struct kwail apple; printf(“구조체릐 크기 : %dn”, sizeof(apple)); } 페이지 279
구조체 배열 구조체의 변수를 하나만 사용하지 않고 여러 개를 사용하려면 일일이 변수 선언 q 이런 불편을 없애기 위해 배열 이용 q struct person { int code; char name[10]; char tel[13]; char addr[10]; }; void main(void) { struct person lee, park, kim; lee. code = 1; strcpy(lee. name, “홍길동”); strcpy(lee. tel, “ 011 -1234 -5678”); strcpy(lee. addr, “경기도”); park. code = 2; strcpy(park. name, “임꺽정”); strcpy(park. tel, “ 018 -1234”); strcpy(park. addr, “경기도”); …………. .
구조체 배열 1 홍길동 Man[0] 011 -1234 -5678 경기도 2 임꺽정 018 -1234 경기도 . . Man[1] struct person { int code; char name[10]; char tel[13]; char addr[10]; }; void main(void) { struct person man[10] = {{1, “홍길동”, “ 011 -1234 -5678”, “경기도”}, {2, “임꺽정”, “ 018 -1234”, “경기도”}}; printf(“전화번호 : %s”, man[0]. tel); man[2]. code = 3; printf(“전화번호 첫자리 : %c”, man[0]. tel[0]);
중첩된 구조체 struct hakbun { int grade; int ban; int bun; }; struct person { struct hakbun hak; char name[10]; char addr[20]; char tel[13]; }; void main(void) { struct person people; people. hak. grade = 3; scanf(“%d”, &people. hak. ban); printf(“%d”, people. hak. bun); scanf(“%s”, people. name); printf(“%s”, people. addr); hak grade ban 페이지 284 bun name addr tel
구조체 포인터 구조체를 포인터 선언하여 사용 q. 대신 ->로 지칭 q struct book { char name[20]; char author[20]; int price; char make[20]; }; void main(void) { struct book bo = {“Visual C++”, “이소미”, 16000, “세일사”}; struct book *boo; boo = &bo; 페이지 286 bo. price = 100000; boo->price = 100000; printf(“%s”, bo. name); printf(“%s”, boo->name);
공용체의 활용 union gong { char ch; int in; double dou; }; union gong go; void main(void) { go. ch = ‘A’; go. in = 1000; 구조체 ch 1 in 2 dou 3 공용체 ch 1 in dou 하나의 메모리를 공용으로 사용 go. dou = 1234. 56; }
열거형의 활용 enum bun {HONG, KIM, LEE=6, PARK, LEEM=KIM+10}; HONG 0, KIM 1, LEE 6 PARK 7, LEEM = 11 enum weekday {Sunday, Monday, Tuesday, Wendsday, Thursday, Friday, Saturday};
Typedef의 활용 struct student { int hakbun; char name[10]; float weight; float height; }; typedef struct student stud; stud aaa; struct student bbb;
- Char char slide
- Ontinue
- Char name 25 charlanguage 10
- Struct student
- Struct student char *name
- How to copy a struct in c
- Typedef struct tree int info
- Typedef struct node
- Struct node int data struct node* next
- Typedef struct tree int info
- Char argv
- Byte short
- Chars = [0] * 128
- Strlen int
- 1 char kaç byte
- Array 4 dimensi
- L char
- Fictional char
- Int ival char cval void *ptr
- Type modifier
- 1 char kaç byte