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)