1 / 10

Computer Programming in C Chapter 6

Computer Programming in C Chapter 6. 2013 년 가을학기 부산대학교 정보컴퓨터공학부. 6 장 . Structure. 목차 Structure 기본 개념 Typedef Pointer to Structure Structure Array Structure 를 이용한 Function Parameter Union Bitwise Structure. 1. Structure 의 기본 개념. 복합적인 데이터 Example:

basil
Télécharger la présentation

Computer Programming in C Chapter 6

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. Computer Programming in C Chapter 6 2013년 가을학기 부산대학교 정보컴퓨터공학부

  2. 6장. Structure • 목차 • Structure 기본 개념 • Typedef • Pointer to Structure • Structure Array • Structure를 이용한 Function Parameter • Union • Bitwise Structure Computer Programming Chapter 6

  3. 1. Structure의 기본 개념 • 복합적인 데이터 • Example: • StudentScore, StudentName, StudentYear… • Structure의 이용 int studentScore[100];int studentYear[100]; char *studentName[100] structure의 member 표지 struct student { int score; int year; char name[50]; }; struct student students[100]; students[i].score=40; students[i].year=4; strcpy(student[i].name,”Lik”); Computer Programming Chapter 6

  4. 2. Typedef • Typedef • User-Defined Type : cf. Built-in Type • More convenient than struct struct student { int score; int year; char name[50]; }; struct student students[100]; New type typedef struct { int score; int year; char name[50]; } student; student students[100]; Computer Programming Chapter 6

  5. 3. Pointer to Structures • Pointer to Structures • Structure Types의 변수가 저장된 주소 typedef struct { int score; int year; char name[50]; } student; student *pstudent; pstudent=malloc(10*sizeof(student)); for(i=0;i<10;i++) { pstudent->year=0; pstudent->score=0; pstudents++; } structure의 member 표시 student students[100]; pstudent=students; for(i=0;i<10;i++) { pstudent->year=0; pstudent->score=0; pstudents++; } student students[100]; pstudent=students; for(i=0;i<10;i++) { pstudent[i].year=0; pstudent[i].score=0; } Computer Programming Chapter 6

  6. 4. Structure Array • 다른 Type과 같이 Array 가능 typedef struct { int score; int year; char name[50]; } student; student students[]= { {3.0, 1, “WG-1”}, {3.5, 1, “WG-2”}, {2.9, 1, “WB-1”}}; struct student { int score; int year; char name[50]; }; struct student students[3]; student *pstud; pstud=students;for(i=0i<3;i++){ pstud->year=0; pstud->score=0.0; pstud++;} for(i=0i<3;i++){ students[i].year=0; students[i].score=0.0;} Computer Programming Chapter 6

  7. 5. Struct와 Function Parameter • Structure를 이용한 Function의 Parameter • 매우 단순 • typedef는 가능한 header file에 정의 • Structure Pointer를 이용한 Function Parameter float Vector10Norm(float v1, float v2, float v3, …, v10) float Vector10Norm(struct vector10 vector) void inputStudent(student *s) { s->year=3; s->score=3.56; strcpy(s->name,”WG-2”); } inputStudent(student &stud); Computer Programming Chapter 6

  8. 6. Union • 경우에 따라 다른 Type을 가지는 것의 Wrapping • Sizeof(Union): 가장 큰 size에 의하여 결정 typedefstruct { char name[50]; int age; char status; /* 0: student, 1: prof. */ union { unsigned int year; /* for student */ char position[20]; /* for professor */ } level;} UnivPerson; UnivPersona; a.status=0; s.level.year=0; Computer Programming Chapter 6

  9. 7. Bitwise Operation • Bit 단위로 변수를 사용 • Structure Type을 이용 typedef struct { unsigned short year:2; /* 00:1, 01:2, 10:3, 11:4 */ unsigned short gender:1; /* 1: female, 0: male */ unsigned short dept:5; /* 000: phy, 001: chem ... */ } studentStatus; Computer Programming Chapter 6

  10. Mask 이용 #define Mask_Year 0x0003 /* 0000 0000 0000 0011 */#define Mask_Gender 0x0004 /* 0000 0000 0000 0100 */ #define Mask_Dept 0x0038 /* 0000 0000 0011 1000 */ #define Male 0x0000#define Female 0x0004 unsigned short studentStatus;studentStatus = (studentStatus & ~Mask_Gender) | Male; . . . if (studentStatus & MaskGender == Male) ... Computer Programming Chapter 6

More Related