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]