Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming in C Chapter 6 2013 년 가을학기 부산대학교 정보컴퓨터공학부.

Similar presentations


Presentation on theme: "Computer Programming in C Chapter 6 2013 년 가을학기 부산대학교 정보컴퓨터공학부."— Presentation transcript:

1 Computer Programming in C Chapter 6 2013 년 가을학기 부산대학교 정보컴퓨터공학부

2 Computer Programming Chapter 62 6 장. Structure 목차  Structure 기본 개념  Typedef  Pointer to Structure  Structure Array  Structure 를 이용한 Function Parameter  Union  Bitwise Structure

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

4 Computer Programming Chapter 64 2. Typedef Typedef User-Defined Type : cf. Built-in Type More convenient than struct struct student { intscore; intyear; charname[50]; }; struct studentstudents[100]; typedef struct { intscore; intyear; charname[50]; } student; studentstudents[100]; New type

5 Computer Programming Chapter 65 3. Pointer to Structures Pointer to Structures Structure Types 의 변수가 저장된 주소 typedef struct { intscore; intyear; charname[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; }

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

7 Computer Programming Chapter 67 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) inputStudent(student &stud); void inputStudent(student *s) { s->year=3; s->score=3.56; strcpy(s->name,”WG-2”); }

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

9 Computer Programming Chapter 69 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;

10 Computer Programming Chapter 610 Mask 이용 #define Mask_Year0x0003 /* 0000 0000 0000 0011 */ #define Mask_Gender0x0004 /* 0000 0000 0000 0100 */ #define Mask_Dept0x0038 /* 0000 0000 0011 1000 */ #define Male0x0000 #define Female0x0004 unsigned short studentStatus; studentStatus = (studentStatus & ~Mask_Gender) | Male;... if (studentStatus & MaskGender == Male)...


Download ppt "Computer Programming in C Chapter 6 2013 년 가을학기 부산대학교 정보컴퓨터공학부."

Similar presentations


Ads by Google