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

  1. <H2>CHAPTER 8: Pointers to Arrays</H2>
  2.  
  3.  
  4.  
  5.     Pointers, of course, can be "pointed at" any type of data
  6. object, including arrays.  While that was evident when we
  7. discussed program 3.1, it is important to expand on how we do
  8. this when it comes to multi-dimensional arrays.
  9.  
  10. <P>
  11.     To review, in Chapter 2 we stated that given an array of
  12. integers we could point an integer pointer at that array using:
  13.  
  14. <PRE>
  15.     int *ptr;
  16.     ptr = &my_array[0];       /* point our pointer at the first
  17.                                  integer in our array */
  18. </PRE>
  19.  
  20. As we stated there, the type of the pointer variable must match
  21. the type of the first element of the array.
  22.  
  23. <P>
  24.  
  25.     In addition, we can use a pointer as a formal parameter of a
  26. function which is designed to manipulate an array.  e.g.
  27.  
  28. <P>
  29.     Given:
  30. <PRE>
  31.     int array[3] = {'1', '5', '7'};
  32.     void a_func(int *p);
  33. </PRE>
  34.  
  35. Some programmers might prefer to write the function prototype as:
  36.  
  37. <PRE>
  38.    void a_func(int p[]);
  39. </PRE>
  40.  
  41. which would tend to inform others who might use this function that the 
  42. function is designed to manipulate the elements of an array.  Of course, in 
  43. either case, what actually gets passed is the value of a pointer to the first 
  44. element of the array, independent of which notation is used in the function
  45. prototype or definition.  Note that if the array notation is used, there is no 
  46. need to pass the actual dimension of the array since we
  47. are not passing the whole array, only the address to the first element.
  48.  
  49.  
  50. <P>
  51.  
  52.     We now turn to the problem of the 2 dimensional array.  As
  53. stated in the last chapter, C interprets a 2 dimensional array as
  54. an array of one dimensional arrays.  That being the case, the
  55. first element of a 2 dimensional array of integers is a one
  56. dimensional array of integers.  And a pointer to a two
  57. dimensional array of integers must be a pointer to that data
  58. type.  One way of accomplishing this is through the use of the
  59. keyword "typedef".  typedef assigns a new name to a specified
  60. data type.  For example:
  61.  
  62. <PRE>
  63.     typedef unsigned char byte;
  64. </PRE>
  65.  
  66. causes the name <B>byte</B> to mean type <B>unsigned char</B>.  Hence
  67.  
  68. <PRE>
  69.     byte b[10];     would be an array of unsigned characters.
  70. </PRE>
  71.  
  72. Note that in the typedef declaration, the word <B>byte</B> has
  73. replaced that which would normally be the name of our <B>unsigned
  74. char</B>.  That is, the rule for using <B>typedef</B> is that the new name
  75. for the data type is the name used in the definition of the data
  76. type.  Thus in:
  77.  
  78. <PRE>
  79.     typedef int Array[10];
  80. </PRE>
  81.  
  82. Array becomes a data type for an array of 10 integers.  i.e.
  83. <B>Array my_arr;</B> declares <B>my_arr</B> as an array of 10 integers and
  84. <B>Array arr2d[5];</B> makes <B>arr2d</B> an array of 5 arrays of 10 integers each.
  85.  
  86. <P>
  87.     Also note that  <B>Array *p1d;</B> makes <B>p1d</B> a pointer to an
  88. array of 10 integers.  Because <B>*p1d</B> points to the same type as
  89. <B>arr2d</B>, assigning the address of the two dimensional array <B>arr2d</B> to
  90. <B>p1d</B>, the pointer to a one dimensional array of 10 integers is
  91. acceptable.  i.e. <B>p1d = &arr2d[0];</B> or <B>p1d = arr2d;</B>  are both correct.
  92.  
  93. <P>
  94.  
  95.     Since the data type we use for our pointer is an array of 10
  96. integers we would expect that incrementing <B>p1d</B> by 1 would change
  97. its value by <B>10*sizeof(int)</B>, which it does.  That is,  <B>sizeof(*p1d)</B>
  98. is 20.  You can prove this to yourself by writing and running a
  99. simple short program.
  100.  
  101. <P>
  102.  
  103.     Now, while using typedef makes things clearer for the reader
  104. and easier on the programmer, it is not really necessary.  What
  105. we need is a way of declaring a pointer like <B>p1d</B> without the need
  106. of the<B> typedef</B> keyword.  It turns out that this can be done and
  107. that
  108.  
  109. <PRE>
  110.     int (*p1d)[10];
  111. </PRE>
  112.  
  113. is the proper declaration, i.e. <B>p1d</B> here
  114. is a pointer to an array of 10 integers just as it was under the
  115. declaration using the Array type.  Note that this is different
  116. from
  117.  
  118. <PRE>
  119.     int *p1d[10];
  120. </PRE>
  121.  
  122. which would make <B>p1d</B> the name of an
  123. array of 10 pointers to type <B>int</B>.
  124.  
  125. <DL>
  126.  
  127. <DT><Center><A HREF=ch9x.htm>Continue with Pointer Tutorial</A></Center>
  128.  
  129. </DT></P>
  130.  
  131. <DT><Center><A Href=pointers.htm>Back to Table of Contents</Center></A><P>
  132.  
  133. </DL>
  134.  
  135.