home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / C / PTRTUT / CH5X.HTM < prev    next >
Text File  |  1997-07-19  |  6KB  |  215 lines

  1. <H2>CHAPTER 5: Pointers and Structures</H2>
  2.  
  3.  
  4.  
  5.     As you may know, we can declare the form of a block of data
  6. containing different data types by means of a structure
  7. declaration.  For example, a personnel file might contain
  8. structures which look something like:
  9.  
  10. <PRE>
  11.     struct tag {
  12.         char lname[20];        /* last name */
  13.         char fname[20];        /* first name */
  14.         int age;               /* age */
  15.         float rate;            /* e.g. 12.75 per hour */
  16.     };
  17. </PRE>
  18.  
  19.     Let's say we have a bunch of these structures in a disk file
  20. and we want to read each one out and print out the first and last
  21. name of each one so that we can have a list of the people in our
  22. files.  The remaining information will not be printed out.  We
  23. will want to do this printing with a function call and pass to
  24. that function a pointer to the structure at hand.  For
  25. demonstration purposes I will use only one structure for now. But
  26. realize the goal is the writing of the function, not the reading
  27. of the file which, presumably, we know how to do.
  28.  
  29. <P>
  30.  
  31.     For review, recall that we can access structure members with
  32. the dot operator as in:
  33.  
  34. <PRE>
  35.  
  36. --------------- program 5.1 ------------------
  37.  
  38. /* Program 5.1 from PTRTUT10.HTM     6/13/97 */
  39.  
  40.  
  41. #include <stdio.h>
  42. #include <string.h>
  43.  
  44. struct tag {
  45.     char lname[20];      /* last name */
  46.     char fname[20];      /* first name */
  47.     int age;             /* age */
  48.     float rate;          /* e.g. 12.75 per hour */
  49. };
  50.  
  51. struct tag my_struct;       /* declare the structure my_struct */
  52.  
  53. int main(void)
  54. {
  55.     strcpy(my_struct.lname,"Jensen");
  56.     strcpy(my_struct.fname,"Ted");
  57.     printf("\n%s ",my_struct.fname);
  58.     printf("%s\n",my_struct.lname);
  59.     return 0;
  60. }
  61.  
  62. -------------- end of program 5.1 --------------
  63.  
  64. </PRE>
  65.  
  66.     Now, this particular structure is rather small compared to
  67. many used in C programs.  To the above we might want to add:
  68.  
  69. <PRE>
  70.     date_of_hire;                  (data types not shown)
  71.     date_of_last_raise;
  72.     last_percent_increase;
  73.     emergency_phone;
  74.     medical_plan;
  75.     Social_S_Nbr;
  76.     etc.....
  77. </PRE>
  78.  
  79.     If we have a large number of employees, what we want to do
  80. manipulate the data in these structures by means of functions.
  81. For example we might want a function print out the name of the
  82. employee listed in any structure passed to it.  However, in the
  83. original C (Kernighan & Ritchie, 1st Edition) it was not possible
  84. to pass a structure, only a pointer to a structure could be
  85. passed.  In ANSI C, it is now permissible to pass the complete
  86. structure. But, since our goal here is to learn more about
  87. pointers, we won't pursue that.
  88.  
  89. <P>
  90.  
  91.     Anyway, if we pass the whole structure it means that we must
  92. copy the contents of the structure from the calling function to
  93. the called function.  In systems using stacks, this is done by
  94. pushing the contents of the structure on the stack.  With large
  95. structures this could prove to be a problem.  However, passing a
  96. pointer uses a minimum amount of stack space.
  97.  
  98. <P>
  99.  
  100.     In any case, since this is a discussion of pointers, we will
  101. discuss how we go about passing a pointer to a structure and then
  102. using it within the function.
  103.  
  104. <P>
  105.  
  106.     Consider the case described, i.e. we want a function that
  107. will accept as a parameter a pointer to a structure and from
  108. within that function we want to access members of the structure.
  109. For example we want to print out the name of the employee in our
  110. example structure.
  111.  
  112. <P>
  113.  
  114.     Okay, so we know that our pointer is going to point to a
  115. structure declared using struct tag.  We declare such a pointer
  116. with the declaration:
  117.  
  118. <PRE>
  119.     struct tag *st_ptr;
  120. </PRE>
  121.  
  122. and we point it to our example structure with:
  123.  
  124. <PRE>
  125.     st_ptr = &my_struct;
  126. </PRE>
  127.  
  128.     Now, we can access a given member by de-referencing the
  129. pointer. But, how do we de-reference the pointer to a structure?
  130. Well, consider the fact that we might want to use the pointer to
  131. set the age of the employee.  We would write:
  132.  
  133. <PRE>
  134.     (*st_ptr).age = 63;
  135. </PRE>
  136.  
  137.     Look at this carefully.  It says, replace that within the
  138. parenthesis with that which <B>st_ptr</B> points to, which is the
  139. structure <B>my_struct</B>.  Thus, this breaks down to the same as
  140. <B>my_struct.age</B>.
  141.  
  142. <P>
  143.  
  144.     However, this is a fairly often used expression and the
  145. designers of C have created an alternate syntax with the same
  146. meaning which is:
  147.  
  148. <PRE>
  149.     st_ptr->age = 63;
  150. </PRE>
  151.  
  152.     With that in mind, look at the following program:
  153.  
  154. <PRE>
  155.  
  156. ------------ program 5.2 ---------------------
  157.  
  158. /* Program 5.2 from PTRTUT10.HTM   6/13/97 */
  159.  
  160. <P>
  161.  
  162. #include <stdio.h>
  163. #include <string.h>
  164.  
  165. struct tag{                     /* the structure type */
  166.     char lname[20];             /* last name */
  167.     char fname[20];             /* first name */
  168.     int age;                    /* age */
  169.     float rate;                 /* e.g. 12.75 per hour */
  170. };
  171.  
  172. struct tag my_struct;           /* define the structure */
  173. void show_name(struct tag *p);  /* function prototype */
  174.  
  175. int main(void)
  176. {
  177.     struct tag *st_ptr;         /* a pointer to a structure */
  178.     st_ptr = &my_struct;        /* point the pointer to my_struct */
  179.     strcpy(my_struct.lname,"Jensen");
  180.     strcpy(my_struct.fname,"Ted");
  181.     printf("\n%s ",my_struct.fname);
  182.     printf("%s\n",my_struct.lname);
  183.     my_struct.age = 63;
  184.     show_name(st_ptr);          /* pass the pointer */
  185.     return 0;
  186. }
  187.  
  188. void show_name(struct tag *p)
  189. {
  190.     printf("\n%s ", p->fname);  /* p points to a structure */
  191.     printf("%s ", p->lname);
  192.     printf("%d\n", p->age);
  193. }
  194.  
  195. -------------------- end of program 5.2 ----------------
  196.  
  197. </PRE>
  198.  
  199.     Again, this is a lot of information to absorb at one time.
  200. The reader should compile and run the various code snippets and
  201. using a debugger monitor things like<B> my_struct</B> and <B>p</B> while single
  202. stepping through the main and following the code down into the
  203. function to see what is happening.
  204.  
  205. <DL>
  206.  
  207. <DT><Center><A HREF=ch6x.htm>Continue with Pointer Tutorial</A></Center>
  208.  
  209. </DT></P>
  210.  
  211. <DT><Center><A Href=pointers.htm>Back to Table of Contents</Center></A><P>
  212.  
  213. </DL>
  214.  
  215.