Arrays are the only type of structure we have used so far. But arrays are limited---all their elements must share the same underlying type. This chapter examines three new ways to represent collections of values: structures, unions, and enumerated types. We introduce structures and show how they group different values in a single variable. We introduce unions and show how they allow us to have a single variable whose value varies in type. And finally, we introduce enumerated types and show how they provide a convenient way to define a set of constants. The chapter concludes with a small database program for storing employee names and phone numbers that comes complete with a menu-driven front end.
Jump to: [Previous Chapter | Next Chapter]
예) 최대 20문자로 된 이름, long integer형의 학번, 실수형의 점수들로
구성된 학생 자료 구조 선언
struct STUDENT {
char name[20];
long id;
float score;
}
예)
struct STUDENT a, b, c, *p;
struct {
char *name;
long id;
float score;
} student[50], *p, x;
예)
struct {
char *name;
long id;
float score;
} student[50], *p, x;
1] x.name = "Hahn";
2] x.id = 9600007L;
3] x.score = 4.5;
4] p = &x;
5] (*p).score ==> p->score ==> 4.5;
6] ++p->id ==> ++(p->id) ==> 9600008L
7] p->name ==> "Hahn"
8] *p->name ==> 'H'
9] *p->name++ ==> 'H', 그리고 name을 증가시켜 "ahn"로 만든다.
10] (*p->name)++ ==> 'I'
11] p = student;
12] (p++)->id ==> student[0].id, 그 후 p == &student[1]
13] (++p)->id ==> ++p->id ==> student[1].id
14] *p++->name
15] *++p->name
struct STUDENT x; struct STUDENT GetStudent(struct STUDENT student[], char *name); x = GetStudent(student, "John");
struct POINT { int x, y; }
struct RECT {
struct POINT p1;
struct POINT p2;
} window;
// 왼쪽 아래 모서리 좌표가 (0, 0), 오른쪽 위 모서리 좌표가 (639, 479)인 window
window.p1.x = 0;
window.p1.y = 0;
window.p2.x = 639;
window.p2.y = 479;
struct RECT r, *rp = &x;
// 다음 4가지 표현들은 같은 결과를 갖는다.
r.p1.x == rp->p1.x ==> (r.p1).x ==> (rp->p1).x
struct STUDENT {
char *name;
long id;
float score;
} student[] = {
"홍길동", 9600001L, 3.5,
"임꺽정", 9600002L, 4.0,
"일지매", 9600007L, 4.5
};
sizeof student ==> 36 bytes (12 * 3)
sizeof student[0] ==> 12 bytes
sizeof (struct STUDENT) ==> 12 bytes
struct STUDENT_LIST {
struct STUDENT student;
struct STUDENT_LIST *next;
};
struct STUDENT_LIST *s;
s = (struct STUDENT_LIST *)malloc(sizeof(struct STUDENT_LIST));
s->student.name = "홍길동"; // (*s). student.name = "홍길동";
s->student.id = 9600007L;
s->student.score = 4.5;
s->next = NULL;
typedef char *string; // string이라는 새로운 type 선언 string s; s = (string)malloc(100); // 100개의 문자를 저장할 메모리 할당 typedef struct STUDENT_LIST *ST_TYPE; ST_TYPE s; s = (ST_TYPE)malloc(sizeof *s); typedef int (*PFI)(char *, char *); PFI fp = strcmp;
예) 구조체를 이용한 날짜 비교 (date.h,
date.c,
datetest.c)
예) 구조체 array 처리 프로그램 (premps.h,
premps.c,
premps1.c,
premps2.c)
union u_tag {
int ival;
float fval;
char *sval;
} u, *p;
u.ival = 1;
p = &u;
p->fval = 3.14;
(*p).sval = "abc";
*(p->sval) = 'A';
*p->sval ==> 'A'
struct {
int type;
union {
int ival;
float fval;
char *sval;
} u;
} u_ary[100];
u_ary[i].u.ival
u_ary[j].u.sval[0]
*u_ary[j].u.sval
예) 기본적인 union 사용(union.c)
예) union을 이용한 다른 형의 자료처리(getvalue.h,
getvalue.c,
getvaluetest.c,
getline.c,
)
struct {
unsigned int : 8;
unsigned int bit7 : 1;
unsigned int bit6 : 1;
unsigned int bit50 : 6;
} flags;
flags.bit7 = 0; // 또는 1 값 저장 가능 (1 bit storage)
flags.bit6 = 1;
flags.bit50 = 63; // 0에서 63까지의 값 저장 가능 (6 bits)
sizeof flags ==> 2 (bytes)
예) 기본적인 bit field 사용(usebits.c)
Ex) 임의의 bit set (1) or clear (0)
Ex) Student record structure를 사용한 binary search
Ex) Linked list와 Tree structure 구성
int binsearch(char *name, STUDENT table[], int n)
{
int cond;
int low, high, mid;
low = 0;
high = n - 1;
while (low <= high) {
mid = (low + high) / 2;
if ((cond = strcmp(name, table[mid].name)) < 0)
high = mid - 1;
else if (cond > 0)
low = mid + 1;
else
return mid;
}
return -1;
}
Ex) 간단한 사원 데이터베이스 관리 프로그램
(db.h,
db.c,
dbupdate.c,
dbprint.c,
dbfind.h,
dbfind.c,
getval2.h,
getval2.c,
getline2.c,
dbinp.h,
dbinp.c)