home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTUTOR1.ZIP / CHAP11.TXT < prev    next >
Encoding:
Text File  |  1986-06-30  |  28.0 KB  |  653 lines

  1.                       Chapter 11 - Structures and Unions
  2.  
  3.  
  4.                             WHAT IS A STRUCTURE?
  5.  
  6.              A structure is a user defined data type.   You have the 
  7.  
  8.         ability  to  define  a new type of  data  considerably  more 
  9.  
  10.         complex than the types we have been using.  A structure is a 
  11.  
  12.         combination  of  several different previously  defined  data 
  13.  
  14.         types,  including other structures we have defined.  An easy 
  15.  
  16.         to  understand definition is,  a structure is a grouping  of 
  17.  
  18.         related  data in a way convenient to the programmer or  user 
  19.  
  20.         of the program.   The best way to understand a structure  is 
  21.  
  22.         to  look  at  an example,  so if you will load  and  display 
  23.  
  24.         STRUCT1.C, we will do just that.
  25.  
  26.              The  program begins with a structure  definition.   The 
  27.  
  28.         key  word  "struct"  is followed by  some  simple  variables 
  29.  
  30.         between  the  braces,   which  are  the  components  of  the 
  31.  
  32.         structure.   After  the  closing brace,  you will  find  two 
  33.  
  34.         variables listed,  namely "boy",  and "girl".   According to 
  35.  
  36.         the  definition  of a structure,  "boy" is  now  a  variable 
  37.  
  38.         composed of three elements,  "initial",  "age", and "grade".  
  39.  
  40.         Each of the three fields are associated with "boy", and each 
  41.  
  42.         can  store a variable of its respective type.   The variable 
  43.  
  44.         "girl"  is also a variable containing three fields with  the 
  45.  
  46.         same  names  as those of "boy" but  are  actually  different 
  47.  
  48.         variables.  We have therefore defined 6 simple variables. 
  49.  
  50.                          A SINGLE COMPOUND VARIABLE
  51.  
  52.              Lets  examine  the  variable "boy"  more  closely.   As 
  53.  
  54.         stated above, each of the three elements of "boy" are simple 
  55.  
  56.         variables  and can be used anywhere in a C program  where  a 
  57.  
  58.         variable of their type can be used.   For example, the "age" 
  59.  
  60.         element  is  an integer variable and can therefore  be  used 
  61.  
  62.         anywhere  in a C program where it is legal to use an integer 
  63.  
  64.         variable,  in calculations, as a counter, in I/O operations, 
  65.  
  66.         etc.   The  only problem we have is defining how to use  the 
  67.  
  68.         simple  variable  "age"  which is a  part  of  the  compound 
  69.  
  70.         variable  "boy".   We  use both names with a  decimal  point 
  71.  
  72.         between  them with the major name first.  Thus "boy.age"  is 
  73.  
  74.         the  complete  variable name for the "age" field  of  "boy".  
  75.  
  76.         This  construct can be used anywhere in a C program that  it 
  77.  
  78.         is desired to refer to this field.   In fact,  it is illegal 
  79.  
  80.         to  use the name "boy" or "age" alone because they are  only 
  81.  
  82.         partial definitions of the complete field.  Alone, the names 
  83.  
  84.         refer to nothing.
  85.  
  86.                      ASSIGNING VALUES TO THE VARIABLES
  87.  
  88.              Using  the above definition,  we can assign a value  to 
  89.  
  90.         each  of  the  three fields of "boy" and each of  the  three 
  91.  
  92.         fields  of  "girl".   Note carefully that  "boy.initial"  is 
  93.  
  94.  
  95.  
  96.                                   Page 73
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.                       Chapter 11 - Structures and Unions
  107.  
  108.  
  109.         actually  a "char" type variable,  because it  was  assigned 
  110.  
  111.         that in the structure, so it must be assigned a character of 
  112.  
  113.         data.   Notice  that "boy.initial" is assigned the character 
  114.  
  115.         'R'  in agreement with the above rules.   The remaining  two 
  116.  
  117.         fields of "boy" are assigned values in accordance with their 
  118.  
  119.         respective  types.   Finally  the three fields of  girl  are 
  120.  
  121.         assigned values but in a different order to illustrate  that 
  122.  
  123.         the order of assignment is not critical.
  124.  
  125.                      HOW DO WE USE THE RESULTING DATA?
  126.  
  127.              Now  that  we  have assigned values to the  six  simple 
  128.  
  129.         variables, we can do anything we desire with them.  In order 
  130.  
  131.         to keep this first example simple,  we will simply print out 
  132.  
  133.         the values to see if they really do exist as  assigned.   If 
  134.  
  135.         you carefully inspect the "printf" statements,  you will see 
  136.  
  137.         that there is nothing special about them.  The compound name 
  138.  
  139.         of each variable is specified because that is the only valid 
  140.  
  141.         name by which we can refer to these variables.
  142.  
  143.              Structures  are  a very useful method of grouping  data 
  144.  
  145.         together  in  order to make a program easier  to  write  and 
  146.  
  147.         understand.   This  first example is too simple to give  you 
  148.  
  149.         even  a hint of the value of using structures,  but continue 
  150.  
  151.         on  through  these lessons and eventually you will  see  the 
  152.  
  153.         value of using structures.
  154.  
  155.              Compile and run STRUCT1.C and observe the output.
  156.  
  157.                            AN ARRAY OF STRUCTURES
  158.  
  159.              Load  and  display the next  program  named  STRUCT2.C.  
  160.  
  161.         This  program  contains  the same  structure  definition  as 
  162.  
  163.         before  but  this  time we define an array of  12  variables 
  164.  
  165.         named "kids".   This program therefore contains 12 times 3 = 
  166.  
  167.         36  simple variables,  each of which can store one  item  of 
  168.  
  169.         data  provided  that  it is of the correct  type.   We  also 
  170.  
  171.         define  a simple variable named "index" for use in  the  for 
  172.  
  173.         loops.
  174.  
  175.              In order to assign each of the fields a value, we use a 
  176.  
  177.         for loop and each pass through the loop results in assigning 
  178.  
  179.         a  value to three of the fields.   One pass through the loop 
  180.  
  181.         assigns all of the values for one of the "kids".  This would 
  182.  
  183.         not be a very useful way to assign data in a real situation, 
  184.  
  185.         but  a loop could read the data in from a file and store  it 
  186.  
  187.         in  the correct fields.   You might consider this the  crude 
  188.  
  189.         beginning of a data base, which it is.
  190.  
  191.              In  the next few instructions of the program we  assign 
  192.  
  193.         new  values to some of the fields to illustrate  the  method 
  194.  
  195.  
  196.  
  197.                                   Page 74
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.                       Chapter 11 - Structures and Unions
  208.  
  209.  
  210.         used to accomplish this.  It should be self explanatory,  so 
  211.  
  212.         no additional comments will be given.
  213.  
  214.                         A NOTE TO PASCAL PROGRAMMERS
  215.  
  216.              Pascal  allows  you to copy an entire RECORD  with  one 
  217.  
  218.         statement.   This is not possible in C.   You must copy each 
  219.  
  220.         element  of a structure one at a time.   As improvements  to 
  221.  
  222.         the   language  are  defined,   this  will  be  one  of  the 
  223.  
  224.         refinements.   In fact,  some of the newer compilers already 
  225.  
  226.         allow   structure   assignment.     Check   your    compiler 
  227.  
  228.         documentation to see if your compiler has this feature yet.
  229.  
  230.                    WE FINALLY DISPLAY ALL OF THE RESULTS
  231.  
  232.              The last few statements contain a for loop in which all 
  233.  
  234.         of  the generated values are displayed in a formatted  list.  
  235.  
  236.         Compile  and  run  the program to see if it  does  what  you 
  237.  
  238.         expect it to do.
  239.  
  240.                    USING POINTERS AND STRUCTURES TOGETHER
  241.  
  242.              Load  and  display  the  file named  STRUCT3.C  for  an 
  243.  
  244.         example of using pointers with structures.   This program is 
  245.  
  246.         identical  to the last program except that it uses  pointers 
  247.  
  248.         for some of the operations.
  249.  
  250.              The  first  difference shows up in  the  definition  of 
  251.  
  252.         variables  following  the  structure  definition.   In  this 
  253.  
  254.         program  we define a pointer named "point" which is  defined 
  255.  
  256.         as  a  pointer that points to the structure.   It  would  be 
  257.  
  258.         illegal  to  try to use this pointer to point to  any  other 
  259.  
  260.         variable  type.   There  is a very definite reason for  this 
  261.  
  262.         restriction  in  C as we have alluded to  earlier  and  will 
  263.  
  264.         review in the next few paragraphs.
  265.  
  266.              The next difference is in the for loop where we use the 
  267.  
  268.         pointer  for accessing the data fields.   Since "kids" is  a 
  269.  
  270.         pointer variable that points to the structure, we can define 
  271.  
  272.         "point"  in  terms  of "kids".   The variable  "kids"  is  a 
  273.  
  274.         constant so it cannot be changed in value,  but "point" is a 
  275.  
  276.         pointer  variable  and can be assigned any value  consistent 
  277.  
  278.         with  its being required to point to the structure.   If  we 
  279.  
  280.         assign  the  value of "kids" to "point" then  it  should  be 
  281.  
  282.         clear  that it will point to the first element of the array, 
  283.  
  284.         a structure containing three fields.
  285.  
  286.                              POINTER ARITHMETIC
  287.  
  288.              Adding  1 to "point" will now cause it to point to  the 
  289.  
  290.         second  field of the array because of the way  pointers  are 
  291.  
  292.  
  293.  
  294.                                   Page 75
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.                       Chapter 11 - Structures and Unions
  305.  
  306.  
  307.         handled in C.   The system knows that the structure contains 
  308.  
  309.         three  variables  and it knows how many memory elements  are 
  310.  
  311.         required to store the complete structure.   Therefore if  we 
  312.  
  313.         tell it to add one to the pointer,  it will actually add the 
  314.  
  315.         number  of  memory  elements  required to get  to  the  next 
  316.  
  317.         element of the array.   If, for example, we were to add 4 to 
  318.  
  319.         the  pointer,  it would advance the value of the  pointer  4 
  320.  
  321.         times the size of the structure,  resulting in it pointing 4 
  322.  
  323.         elements  farther  along the array.   This is the  reason  a 
  324.  
  325.         pointer  cannot be used to point to any data type other than 
  326.  
  327.         the one for which it was defined.
  328.  
  329.              Now to return to the program displayed on your monitor.  
  330.  
  331.         It  should be clear from the previous discussion that as  we 
  332.  
  333.         go through the loop, the pointer will point to the beginning 
  334.  
  335.         of  one of the array elements each time.   We can  therefore 
  336.  
  337.         use  the  pointer to reference the various elements  of  the 
  338.  
  339.         structure.   Referring to the elements of a structure with a 
  340.  
  341.         pointer occurs so often in C that a special method of  doing 
  342.  
  343.         that  was  devised.   Using "point->initial" is the same  as 
  344.  
  345.         using  "(*point).initial" which is really the way we did  it 
  346.  
  347.         in the last two programs.   Remember that *point is the data 
  348.  
  349.         to  which  the pointer points and the  construct  should  be 
  350.  
  351.         clear.   The  "->"  is  made up of the minus  sign  and  the 
  352.  
  353.         greater than sign. 
  354.  
  355.              Since the pointer points to the structure, we must once 
  356.  
  357.         again  define which of the elements we wish to refer to each 
  358.  
  359.         time  we use one of the elements of  the  structure.   There 
  360.  
  361.         are, as we have seen, several different methods of referring 
  362.  
  363.         to  the members of the structure,  and in the for loop  used 
  364.  
  365.         for output at the end of the program, we use three different 
  366.  
  367.         methods.   This  would  be considered very poor  programming 
  368.  
  369.         practice,  but  is done this way here to illustrate  to  you 
  370.  
  371.         that  they all lead to the same result.   This program  will 
  372.  
  373.         probably   require   some  study  on  your  part  to   fully 
  374.  
  375.         understand,  but  it will be worth your time and  effort  to 
  376.  
  377.         grasp these principles.
  378.  
  379.              Compile and run this program.
  380.  
  381.                         NESTED AND NAMED STRUCTURES
  382.  
  383.              Load and display the file named NESTED.C for an example 
  384.  
  385.         of  a nested structure.   The structures we have seen so far 
  386.  
  387.         have been very simple,  although useful.   It is possible to 
  388.  
  389.         define  structures  containing dozens and even  hundreds  or 
  390.  
  391.         thousands  of  elements but it would be to  the  programmers 
  392.  
  393.         advantage not to define all of the elements at one pass  but 
  394.  
  395.         rather to use a hierarchical structure of definition.   This 
  396.  
  397.         will be illustrated with the program on your monitor.
  398.  
  399.  
  400.  
  401.                                   Page 76
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.                       Chapter 11 - Structures and Unions
  412.  
  413.  
  414.  
  415.              The  first  structure  contains three elements  but  is 
  416.  
  417.         followed by no variable name.  We therefore have not defined 
  418.  
  419.         any variables only a structure, but since we have included a 
  420.  
  421.         name  at the beginning of the structure,  the  structure  is 
  422.  
  423.         named  "person".   The name "person" can be used to refer to 
  424.  
  425.         the  structure  but not to any variable  of  this  structure 
  426.  
  427.         type.   It is therefore a new type that we have defined, and 
  428.  
  429.         we can use the new type in nearly the same way we use "int", 
  430.  
  431.         "char",  or  any  other  types that exist in  C.   The  only 
  432.  
  433.         restriction is that this new name must always be  associated 
  434.  
  435.         with the reserved word "struct".
  436.  
  437.              The  next  structure definition contains  three  fields 
  438.  
  439.         with the middle field being the previously defined structure 
  440.  
  441.         which we named "person".  The variable which has the type of 
  442.  
  443.         "person" is named "descrip".   So the new structure contains 
  444.  
  445.         two   simple   variables,   "grade"  and  a   string   named 
  446.  
  447.         "lunch[25]",  and  the  structure  named  "descrip".   Since 
  448.  
  449.         "descrip"  contains  three  variables,   the  new  structure 
  450.  
  451.         actually contains 5 variables.  This structure is also given 
  452.  
  453.         a name "alldat",  which is another type definition.  Finally 
  454.  
  455.         we  define an array of 53 variables each with the  structure 
  456.  
  457.         defined by "alldat",  and each with the name "student".   If 
  458.  
  459.         that is clear,  you will see that we have defined a total of 
  460.  
  461.         53 times 5 variables,  each of which is capable of storing a 
  462.  
  463.         value.
  464.  
  465.                              TWO MORE VARIABLES
  466.  
  467.              Since  we  have a new type definition we can use it  to 
  468.  
  469.         define  two  more variables.   The variables  "teacher"  and 
  470.  
  471.         "sub"  are defined in the next statement to be variables  of 
  472.  
  473.         the  type  "alldat",  so that each of  these  two  variables 
  474.  
  475.         contain 5 fields which can store data.
  476.  
  477.                        NOW TO USE SOME OF THE FIELDS
  478.  
  479.              In  the next five lines of the program,  we will assign 
  480.  
  481.         values to each of the fields of "teacher".   The first field 
  482.  
  483.         is  the  "grade" field and is handled just  like  the  other 
  484.  
  485.         structures  we  have studied because it is not part  of  the 
  486.  
  487.         nested structure.  Next we wish to assign a value to her age 
  488.  
  489.         which  is  part of the nested structure.   To  address  this 
  490.  
  491.         field  we start with the variable name "teacher" to which we 
  492.  
  493.         append  the name of the group "descrip",  and then  we  must 
  494.  
  495.         define which field of the nested structure we are interested 
  496.  
  497.         in,  so  we append the name "age".   The teachers status  is 
  498.  
  499.         handled in exactly the same manner as her age,  but the last 
  500.  
  501.         two  fields  are  assigned  strings using  the  string  copy 
  502.  
  503.         "strcpy" function which must be used for string  assignment.  
  504.  
  505.  
  506.  
  507.                                   Page 77
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.                       Chapter 11 - Structures and Unions
  518.  
  519.  
  520.         Notice  that the variable names in the "strcpy" function are 
  521.  
  522.         still variable names even though they are made up of several 
  523.  
  524.         parts each.
  525.  
  526.              The variable "sub" is assigned nonsense values in  much 
  527.  
  528.         the  same  way,  but in a different order since they do  not 
  529.  
  530.         have to occur in any required order.   Finally, a few of the 
  531.  
  532.         "student"  variables  are assigned values  for  illustrative 
  533.  
  534.         purposes  and  the program ends.   None of  the  values  are 
  535.  
  536.         printed  for illustration since several were printed in  the 
  537.  
  538.         last examples.
  539.  
  540.              Compile  and run this program,  but when you run it you 
  541.  
  542.         may get a "stack overflow" error.   C uses it's own internal 
  543.  
  544.         stack  to  store  the  automatic variables  on  but  most  C 
  545.  
  546.         compilers  use only a 2048 byte stack as a  default.    This 
  547.  
  548.         program  has more than that in the defined structures so  it 
  549.  
  550.         will be necessary for you to increase the stack size.    The 
  551.  
  552.         method  for  doing this for some compilers is given  in  the 
  553.  
  554.         accompanying COMPILER.DOC file with this tutorial.   Consult 
  555.  
  556.         your compiler documentation for details about your compiler.  
  557.  
  558.         There  is  another way around this problem,  and that is  to 
  559.  
  560.         move the structure definitions outside of the program  where 
  561.  
  562.         they will be external variables and therefore static.    The 
  563.  
  564.         result  is that they will not be kept on the internal  stack 
  565.  
  566.         and  the  stack will therefore not overflow.    It would  be 
  567.  
  568.         good for you to try both methods of fixing this problem.
  569.  
  570.                            MORE ABOUT STRUCTURES
  571.  
  572.              It is possible to continue nesting structures until you 
  573.  
  574.         get  totally confused.   If you define  them  properly,  the 
  575.  
  576.         computer  will  not get confused because there is no  stated 
  577.  
  578.         limit as to how many levels of nesting are  allowed.   There 
  579.  
  580.         is probably a practical limit of three beyond which you will 
  581.  
  582.         get confused, but the language has no limit.  In addition to 
  583.  
  584.         nesting, you can include as many structures as you desire in 
  585.  
  586.         any level of structures,  such as defining another structure 
  587.  
  588.         prior  to  "alldat" and using it in "alldat" in addition  to 
  589.  
  590.         using  "person".   The  structure named  "person"  could  be 
  591.  
  592.         included in "alldat" two or more times if desired,  as could 
  593.  
  594.         pointers to it. 
  595.  
  596.              Structures can contain arrays of other structures which 
  597.  
  598.         in  turn  can  contain  arrays  of  simple  types  or  other 
  599.  
  600.         structures.   It  can go on and on until you lose all reason 
  601.  
  602.         to  continue.   I am only trying to illustrate to  you  that 
  603.  
  604.         structures  are  very valuable and you will find them  great 
  605.  
  606.         aids to programming if you use them wisely.  Be conservative 
  607.  
  608.         at first, and get bolder as you gain experience.
  609.  
  610.  
  611.  
  612.  
  613.                                   Page 78
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.                       Chapter 11 - Structures and Unions
  624.  
  625.  
  626.              More  complex structures will not be illustrated  here, 
  627.  
  628.         but  you will find examples of additional structures in  the 
  629.  
  630.         example  programs  included  in the  last  chapter  of  this 
  631.  
  632.         tutorial.     For   example,   see   the   "#include"   file 
  633.  
  634.         "STRUCT.DEF". 
  635.  
  636.                               WHAT ARE UNIONS?
  637.  
  638.              Load the file named UNION1.C for an example of a union.  
  639.  
  640.         Simply stated,  a union allows you a way to look at the same 
  641.  
  642.         data  with  different types,  or to use the same  data  with 
  643.  
  644.         different names. Examine the program on your monitor. 
  645.  
  646.              In this example we have two elements to the union,  the 
  647.  
  648.         first part being the integer named "value",  which is stored 
  649.  
  650.         as  a  two byte variable somewhere in the computers  memory.  
  651.  
  652.         The  second  element is made up of two  character  variables 
  653.  
  654.         named "first" and "second".   These two variables are stored 
  655.  
  656.         in  the  same storage locations that "value" is  stored  in, 
  657.  
  658.         because  that is what a union does.   A union allows you  to 
  659.  
  660.         store  different types of data in the same physical  storage 
  661.  
  662.         locations.  In this case, you could put an integer number in 
  663.  
  664.         "value",  then retrieve it in its two halves by getting each 
  665.  
  666.         half  using  the  two  names  "first"  and  "second".   This 
  667.  
  668.         technique is often used to pack data bytes together when you 
  669.  
  670.         are,  for  example,  combining  bytes  to  be  used  in  the 
  671.  
  672.         registers of the microprocessor.  
  673.  
  674.              Accessing  the fields of the union are very similar  to 
  675.  
  676.         accessing the fields of a structure and will be left to  you 
  677.  
  678.         to determine by studying the example.
  679.  
  680.              One  additional  note  must  be given  here  about  the 
  681.  
  682.         program.  When it is run using most compilers, the data will 
  683.  
  684.         be  displayed  with two leading f's due to  the  hexadecimal 
  685.  
  686.         output  promoting  the  char  type  variables  to  int   and 
  687.  
  688.         extending  the  sign bit to the left.   Converting the  char 
  689.  
  690.         type data fields to int type fields prior to display  should 
  691.  
  692.         remove the leading f's from your display.  This will involve 
  693.  
  694.         defining  two new int type variables and assigning the  char 
  695.  
  696.         type  variables to them.   This will be left as an  exercise 
  697.  
  698.         for  you.   Note that the same problem will come up in a few 
  699.  
  700.         of the later files also. 
  701.  
  702.              Compile and run this program and observe that the  data 
  703.  
  704.         is  read out as an "int" and as two "char"  variables.   The 
  705.  
  706.         "char" variables are reversed in order because of the way an 
  707.  
  708.         "int" variable is stored internally in your computer.  Don't 
  709.  
  710.         worry  about this.  It is not a problem but it can be a very 
  711.  
  712.         interesting area of study if you are so inclined.
  713.  
  714.  
  715.  
  716.  
  717.                                   Page 79
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.                       Chapter 11 - Structures and Unions
  728.  
  729.  
  730.                            ANOTHER UNION EXAMPLE
  731.  
  732.              Load  and  display the file named UNION2.C for  another 
  733.  
  734.         example of a union,  one which is much more common.  Suppose 
  735.  
  736.         you  wished to build a large database including  information 
  737.  
  738.         on many types of vehicles.  It would be silly to include the 
  739.  
  740.         number of propellers on a car,  or the number of tires on  a 
  741.  
  742.         boat.   In  order to keep all pertinent data,  however,  you 
  743.  
  744.         would  need  those  data points for their  proper  types  of 
  745.  
  746.         vehicles.   In  order to build an efficient data  base,  you 
  747.  
  748.         would need several different types of data for each vehicle, 
  749.  
  750.         some  of which would be common,  and some of which would  be 
  751.  
  752.         different.  That is exactly what we are doing in the example 
  753.  
  754.         program on your monitor.
  755.  
  756.              In this program,  we will define a complete  structure, 
  757.  
  758.         then  decide which of the various types can go into it.   We 
  759.  
  760.         will  start at the top and work our  way  down.   First,  we 
  761.  
  762.         define  a  few constants with the #defines,  and  begin  the 
  763.  
  764.         program  itself.   We define a structure named  "automobile" 
  765.  
  766.         containing  several fields which you should have no  trouble 
  767.  
  768.         recognizing, but we define no variables at this time.
  769.  
  770.                          A NEW CONCEPT, THE TYPEDEF
  771.  
  772.              Next  we  define a new type of data with  a  "typedef".  
  773.  
  774.         This  defines  a complete new type that can be used  in  the 
  775.  
  776.         same way that "int" or "char" can be used.   Notice that the 
  777.  
  778.         structure  has  no name,  but at the end where  there  would 
  779.  
  780.         normally be a variable name there is the name "BOATDEF".  We 
  781.  
  782.         now have a new type, "BOATDEF", that can be used to define a 
  783.  
  784.         structure anyplace we would like to.   Notice that this does 
  785.  
  786.         not  define  any  variables,  only a  new  type  definition. 
  787.  
  788.         Capitalizing  the name is a personal preference only and  is 
  789.  
  790.         not  a  C standard.   It makes the "typedef" look  different 
  791.  
  792.         from a variable name.
  793.  
  794.              We  finally come to the big structure that defines  our 
  795.  
  796.         data using the building blocks already defined  above.   The 
  797.  
  798.         structure is composed of 5 parts, two simple variables named 
  799.  
  800.         "vehicle" and "weight",  followed by the union,  and finally 
  801.  
  802.         the last two simple variables named "value" and "owner".  Of 
  803.  
  804.         course  the union is what we need to look at carefully here, 
  805.  
  806.         so focus on it for the moment.   You will notice that it  is 
  807.  
  808.         composed  of four parts,  the first part being the  variable 
  809.  
  810.         "car" which is a structure that we defined previously.   The 
  811.  
  812.         second  part is a variable named "boat" which is a structure 
  813.  
  814.         of the type "BOATDEF" previously defined.  The third part of 
  815.  
  816.         the  union is the variable "airplane" which is  a  structure 
  817.  
  818.         defined in place in the union.   Finally we come to the last 
  819.  
  820.  
  821.  
  822.  
  823.  
  824.                                   Page 80
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.                       Chapter 11 - Structures and Unions
  835.  
  836.  
  837.         part  of  the  union,  the variable named  "ship"  which  is 
  838.  
  839.         another structure of the type "BOATDEF".
  840.  
  841.              I  hope  it is obvious to you that all four could  have 
  842.  
  843.         been defined in any of the three ways shown,  but the  three 
  844.  
  845.         different  methods  were used to show you that any could  be 
  846.  
  847.         used.   In practice,  the clearest definition would probably 
  848.  
  849.         have occurred by using the "typedef" for each of the parts.
  850.  
  851.                             WHAT DO WE HAVE NOW?
  852.  
  853.              We  now have a structure that can be used to store  any 
  854.  
  855.         of  four different kinds of data structures.   The  size  of 
  856.  
  857.         every  record will be the size of that record containing the 
  858.  
  859.         largest  union.   In this case part 1 is the  largest  union 
  860.  
  861.         because  it is composed of three integers,  the others being 
  862.  
  863.         composed  of  an integer and a character  each.   The  first 
  864.  
  865.         member  of this union would therefore determine the size  of 
  866.  
  867.         all structures of this type.  The resulting structure can be 
  868.  
  869.         used to store any of the four types of data, but it is up to 
  870.  
  871.         the  programmer  to  keep track of what is  stored  in  each 
  872.  
  873.         variable of this type.   The variable "vehicle" was designed 
  874.  
  875.         into  this  structure to keep track of the type  of  vehicle 
  876.  
  877.         stored here.   The four defines at the top of the page  were 
  878.  
  879.         designed  to  be  used  as indicators to be  stored  in  the 
  880.  
  881.         variable "vehicle". 
  882.          
  883.              A  few  examples of how to use the resulting  structure 
  884.  
  885.         are given in the next few lines of the program.  Some of the 
  886.  
  887.         variables are defined and a few of them are printed out  for 
  888.  
  889.         illustrative purposes. 
  890.  
  891.              The union is not used too frequently,  and almost never 
  892.  
  893.         by   beginning   programmers.    You   will   encounter   it 
  894.  
  895.         occasionally  so  it is worth your effort to at  least  know 
  896.  
  897.         what  it is.   You do not need to know the details of it  at 
  898.  
  899.         this time,  so don't spend too much time studying it.   When 
  900.  
  901.         you do have a need for a variant structure, a union, you can 
  902.  
  903.         learn it at that time. For your own benefit, however, do not 
  904.  
  905.         slight the structure. You should use the structure often.
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.         PROGRAMMING EXERCISES
  914.  
  915.         1.   Define a named structure  containing a string field for 
  916.  
  917.              a name,  an integer for feet, and another for arms. Use 
  918.  
  919.  
  920.  
  921.                                   Page 81
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931.                       Chapter 11 - Structures and Unions
  932.  
  933.  
  934.              the new type to define an array of about 6 items.  Fill 
  935.  
  936.              the fields with data and print them out as follows.
  937.  
  938.              A human being has 2 legs and 2 arms.
  939.              A dog has 4 legs and 0 arms.
  940.              A television set has 4 legs and 0 arms.
  941.              A chair has 4 legs and 2 arms.
  942.              etc.
  943.  
  944.         2.   Rewrite  exercise 1 using  a pointer to print the  data 
  945.  
  946.              out.
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.                                   Page 82
  990.  
  991.