1 / 18

13 장 . 구조체와 공용체

13 장 . 구조체와 공용체. 구조체. struct 태그명 { 항목선언목록 }; struct 태그명 구조체변수명 ; struct { 항목선언목록 } 구조체변수명 ;. 구조체. struct student_record { int student_id; char name[name_size]; float GPA; }; struct student_record student;. 구조체. struct student_record { int student_id;

Download Presentation

13 장 . 구조체와 공용체

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 13장. 구조체와 공용체 Ch.13

  2. 구조체 • struct태그명 {항목선언목록}; struct태그명 구조체변수명; • struct {항목선언목록} 구조체변수명; Ch.13

  3. 구조체 struct student_record { int student_id; char name[name_size]; float GPA; }; struct student_record student; Ch.13

  4. 구조체 struct student_record { int student_id; char name[name_size]; float GPA; }; struct student_record student; • scanf("%d", &student.student_id); • printf("%d", student.student_id); Ch.13

  5. 구조체 struct student_record { /* 구조체 정의 */ int student_id; char name[name_size]; float GPA; }; struct student_record student[10]; /* 구조체 배열 student 선언 / • printf("%d", student[i].student_id); Ch.13

  6. 구조체 struct student_record { /* 구조체 정의 */ int student_id; char name[name_size]; float GPA; }; struct student_record student[max_num]= { {1234, “홍길동”, 4.0}, /* student의 초기화 */ {1235, “고길동”, 3.0}, ... {1300, “마동탁”, 2.0} }; Ch.13

  7. 구조체 • student[0].student_id = 1234; • strcpy(student[0].name, “홍길동”); • student[0].GPA = 4.0; Ch.13

  8. 구조체 struct address_record { char country[10]; char city[10]; char address[20]; }; struct student_record { int student_id; char name[name_size]; struct address_record location; /* 구조체 내포 */ float GPA; }; • student[i].location.city Ch.13

  9. 구조체 #include <stdio.h> #define str_size1 20 #define str_size2 10 #define stu_num 2 /* 학생 수 */ struct address_record { char country[str_size2]; char city[str_size2]; char address[str_size1]; }; struct student_record { int student_id; char name[str_size1]; struct address_record location; /* 구조체 내포 */ float GPA; }; Ch.13

  10. 구조체 void main() { int i; struct student_record course[stu_num]= { /* 학생 2명 기록 초기화 */ {1234, "KIM", {"KOREA", "SEOUL", "SAMSUN 123"}, 4.0}, {1235, "PARK", {"KOREA", "PUSAN", "KWANGBOK 124"}, 3.0} }; struct student_record *course_ptr; /* 구조체에 대한 포인터 변수 */ for(i= 0; i < stu_num ; i++) printf("%2d번째 record의 주소:%u \n", i, &course[i]); course_ptr= course;/* course_ptr= &course[0];과 동일한 표현 */ for(i= 0; i < stu_num ; i++) printf("%2d번째 record의 주소:%u \n", i, course_ptr+i); printf("첫 번째 학생의 ID:%d국적: %s \n", course_ptr->student_id, course_ptr->location.country ); printf("두 번째 학생의 ID:%d국적: %s \n", (*(course_ptr+1)).student_id, (*(course_ptr+1)).location.country ); } Ch.13

  11. 구조체 • course_ptr -> student_id == course[0].student_id • (*course_ptr).student_id ==course[0].student_id Ch.13

  12. 구조체 • void update(struct student_record cls[]) { . . . scanf("%d", &(cls[0].student_id)); . . . } • void update(struct student_record *cls) { . . . scanf("%d", &(cls->student_id)); . . . } • struct student_record course[2]; . . . update(course); /* 함수 호출 */ Ch.13

  13. 구조체 float GPA_AVG(struct student_record *cls) { int i; float sum= 0.0; for (i= 0; i < stu_num; i++, cls++) sum+= cls->GPA; return(sum/stu_num); } Ch.13

  14. 공용체 #include <stdio.h> union int_float { int i; float j; /* i와 j는 공간을 공유 */ } number; /* 공용체 int_float의 변수 number 선언 */ void main() { number.i= 10; printf("I= %10d J= %15.10e∖n", number.i, number.j); // I= 10 J= 1.4012984643e-44 number.j= 10.0; printf("I= %10d J= %15.10e∖n", number.i, number.j); // I= 0 J= 1.0000000000e+01 } // 공용체 내부는 같은 자료형이어야 한다. Ch.13

  15. Typedef (자료형의 사용자 정의) • typedef float real; • real x, y, *pt; /* float형 변수를 선언 */ Ch.13

  16. typedef typedef struct student_record { int student_id; char name[name_size]; struct address_record location; float GPA; } STUDENT; /* 구조체 이름을 STUDENT로 */ • STUDENT st1, st2; /* 구조체 변수들의 선언 */ Ch.13

  17. typedef • #define STUDENT structure student_record ... • STUDENT st1, st2; Ch.13

  18. typedef • typedef char *STRING; . . . • STRING name, address; • char *name, *address; Ch.13

More Related