computer programming in c chapter 6
DESCRIPTION
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: - PowerPoint PPT PresentationTRANSCRIPT
Computer Programming in C Chapter 6
2013 년 가을학기부산대학교
정보컴퓨터공학부
Computer Programming Chapter 6 2
6 장 . Structure
목차1. Structure 기본 개념2. Typedef3. Pointer to Structure4. Structure Array5. Structure 를 이용한 Function Parameter6. Union7. Bitwise Structure
Computer Programming Chapter 6 3
1. Structure 의 기본 개념
복합적인 데이터 Example:
StudentScore, StudentName, StudentYear…
Structure 의 이용struct student { int score; int year; char name[50];};
struct student students[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 표지
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];
typedef struct { int score; int year; char name[50];} student;
student students[100];
New type
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 가능
struct student { int score; int year; char name[50];};
struct student students[3];
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”}};
for(i=0i<3;i++){ students[i].year=0; students[i].score=0.0;}
student *pstud;
pstud=students;for(i=0i<3;i++){ pstud->year=0; pstud->score=0.0; pstud++;}
Computer Programming Chapter 6 7
5. Struct 와 Function Parame-ter Structure 를 이용한 Function 의 Param-
eter 매우 단순
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”);}
Computer Programming Chapter 6 8
6. Union
경우에 따라 다른 Type 을 가지는 것의 Wrapping
Sizeof(Union): 가장 큰 size 에 의하여 결정
typedef struct { char name[50]; int age; char status; /* 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;
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) ...