CHAPTER 12 
CONSTRUCTED TYPES

[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]
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]


  1. STRUCTURES
  2. BITFIELDS
  3. UNIONS
  4. ENUMERATED TYPES
  5. CASE STUDY: A DATABASE APPLICATION

Objectives


 

Structure: a collection of one or more variables

  • ¿©·¯ ´Ù¸¥ ÇüÀÇ º¯¼öµéÀÇ ¸ðÀÓÀ» ÇϳªÀÇ µ¶¸³µÈ ¾çÀ¸·Î Ãë±ÞÇÒ ¼ö ÀÖ°Ô ÇÔÀ¸·Î½á º¹ÀâÇÑ ÀÚ·á 󸮸¦ ÆíÇÏ°Ô ÇØ ÁØ´Ù.

  •  

     

    Structure ¼±¾ð

  • struct TagName { member list };

  • ¿¹) ÃÖ´ë 20¹®ÀÚ·Î µÈ À̸§, long integerÇüÀÇ Çйø, ½Ç¼öÇüÀÇ Á¡¼öµé·Î ±¸¼ºµÈ Çлý ÀÚ·á ±¸Á¶ ¼±¾ð


     

    Structure º¯¼ö ¼±¾ð

  • struct TagName º¯¼ö¸í;
  • struct { member_list } º¯¼ö¸í;

  •  

     

    ¿¹)


     

    StructureÀÇ member º¯¼ö »ç¿ë

  • structure_variable_name.member_name
  • structure_pointer->member_name

  • ¿¹)


     

    Structure »ç¿ë

    1. Structure assignment: µ¿ÀÏÇÑ structure´Â Á÷Á¢ assignÀÌ °¡´É
    2. Function arguments ¶Ç´Â return typeÀ¸·Î »ç¿ë

    Nested structures


     

    Structure ÃʱâÈ­


     

    Self-referenctial structure (Linked list¿¡ »ç¿ë)


     

    Typedef¸¦ ÀÌ¿ëÇÑ structureÇüÀÇ data structure ¼±¾ð


     

    ¿¹) ±¸Á¶Ã¼¸¦ ÀÌ¿ëÇÑ ³¯Â¥ ºñ±³ (date.h, date.c, datetest.c)
     
     
     

    ¿¹) ±¸Á¶Ã¼ array ó¸® ÇÁ·Î±×·¥ (premps.h, premps.c, premps1.c, premps2.c)
     
     
     

    Unions: ÇÑ º¯¼ö·Î ¿©·¯ ÇüÀÇ ÀڷḦ ó¸®ÇÒ ¼ö ÀÖ°Ô ÇÔ.

  • Compiler°¡ Á¦ÀÏ Å« ÀÚ·áÇüÀ» ÀúÀåÇÒ ¼ö ÀÖ´Â ¸Þ¸ð¸® ÇÒ´ç

  •  
     

    ¿¹) ±âº»ÀûÀÎ union »ç¿ë(union.c)
     
     
     

    ¿¹) unionÀ» ÀÌ¿ëÇÑ ´Ù¸¥ ÇüÀÇ ÀÚ·áó¸®(getvalue.h, getvalue.c, getvaluetest.c, getline.c, )
     
     
     

    Bit-fields : bit ´ÜÀ§ÀÇ access ó¸®.

  • Implementation-dependent

  •  

    ¿¹) ±âº»ÀûÀÎ bit field »ç¿ë(usebits.c)
     
     
     

    Ex) ÀÓÀÇÀÇ bit set (1) or clear (0)

    Ex) Student record structure¸¦ »ç¿ëÇÑ binary search

    Ex) Linked list¿Í Tree structure ±¸¼º


     

    Ex) °£´ÜÇÑ »ç¿ø µ¥ÀÌÅͺ£À̽º °ü¸® ÇÁ·Î±×·¥
    (db.h, db.c, dbupdate.c, dbprint.c, dbfind.h, dbfind.c, getval2.h, getval2.c, getline2.c, dbinp.h, dbinp.c)
     
     


    Assignment #10 (Á¦Ãâ ±âÇÑ: 2ÁÖÀÏ)

    Çлý À̸§, Çйø, Á¡¼ö·Î ±¸¼ºµÈ Çлý °ü·Ã Á¤º¸ ó¸® ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó.


    [ Table Of Contents | Previous Chapter | Next Chapter]