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

  1. <H2>CHAPTER 7: More on Multi-Dimensional Arrays</H2>
  2.  
  3.     In the previous chapter we noted that given
  4.  
  5. <PRE>
  6.     #define ROWS 5
  7.     #define COLS 10
  8.  
  9.     int multi[ROWS][COLS];
  10. </PRE>
  11.  
  12. we can access individual elements of the array <B>multi </B>using
  13. either:
  14.  
  15. <PRE>
  16.     multi[row][col]
  17. </PRE>
  18.  
  19. or
  20.  
  21. <PRE>
  22.     *(*(multi + row) + col)
  23. </PRE>
  24.  
  25. To understand more fully what is going on, let us replace
  26.  
  27. <PRE>
  28.     *(multi + row)
  29. </PRE>
  30.  
  31. with  <B> X</B>   as in:
  32.  
  33. <PRE>
  34.     *(X + col)
  35. </PRE>
  36.  
  37.     Now, from this we see that <B>X</B> is like a pointer since the
  38. expression is de-referenced and we know that <B>col</B> is an integer.
  39. Here the arithmetic being used is of a special kind called
  40. "pointer arithmetic" is being used.  That means that, since we
  41. are talking about an integer array, the address pointed to by
  42. (i.e. value of)   <B>X + col + 1</B>   must be greater than the address
  43. <B>X + col</B>   by and amount equal to <B>sizeof(int)</B>.
  44.  
  45. <P>
  46.  
  47.     Since we know the memory layout for 2 dimensional arrays, we
  48. can determine that in the expression     <B>multi + row</B>    as used
  49. above,   <B>multi + row + 1</B>    must increase by value an amount
  50. equal to that needed to "point to" the next row, which in this
  51. case would be an amount equal to     <B>COLS * sizeof(int)</B>.
  52.  
  53. <P>
  54.  
  55.     That says that if the expression     <B>*(*(multi + row) + col)</B>
  56. is to be evaluated correctly at run time, the compiler must
  57. generate code which takes into consideration the value of <B>COLS</B>,
  58. i.e. the 2nd dimension.  Because of the equivalence of the two
  59. forms of expression, this is true whether we are using the
  60. pointer expression as here or the array expression
  61. <B>multi[row][col]</B>.
  62.  
  63. <P>
  64.  
  65. Thus, to evaluate either expression, a total of 5 values must be
  66.  
  67. known:
  68.  
  69. <OL>
  70.  
  71. <LI>The address of the first element of the array, which is
  72.         returned by the expression  <B>multi</B>, i.e., the name of the
  73.         array.
  74.  
  75. <LI>The size of the type of the elements of the array,  in
  76.         this case <B>sizeof(int)</B>.
  77.  
  78. <LI>The 2nd dimension of the array
  79.  
  80. <LI>The specific index value for the first dimension, <B>row</B>
  81.         in this case.
  82.  
  83. <LI>The specific index value for the second dimension, <B>col</B>
  84.         in this case.
  85.  
  86. </OL>
  87.  
  88.     Given all of that, consider the problem of designing a
  89. function to manipulate the element values of a previously
  90. declared array. For example, one which would set all the elements
  91. of the array <B>multi</B> to the value 1.
  92.  
  93. <PRE>
  94.  
  95.     void set_value(int m_array[][COLS])
  96.     {
  97.         int row, col;
  98.         for (row = 0; row < ROWS; row++)
  99.         {
  100.             for (col = 0; col < COLS; col++)
  101.             {
  102.                 m_array[row][col] = 1;
  103.             }
  104.         }
  105.     }
  106.  
  107. </PRE>
  108.  
  109. And to call this function we would then use:
  110.  
  111. <PRE>
  112.     set_value(multi);
  113. </PRE>
  114.  
  115.  Now, within the function we have used the values #defined by ROWS and 
  116. COLS that set the limits on the for loops. But, these #defines are just constants 
  117. as far as the compiler is concerned, i.e. there is nothing to connect them to the 
  118. array size within the function. <B>row</B> and <B>col</B> are local variables, of course. The 
  119. formal parameter definition permits the compiler to determine the characteristics 
  120. associated with the pointer value that will be passed at run time.  We really 
  121. donÆt need the first dimension and, as will be seen later, there are occasions 
  122. where we would prefer not to define it within the parameter definition, out 
  123. of habit or consistency, I have not used it here. But, the second dimension 
  124. must be used as has been shown in the expression for the parameter. The 
  125. reason is that we need this in the evaluation of <B>m_array[row][col]</B> as has been 
  126. described. While the parameter defines the data type (<B>int</B> in this case) and the 
  127. automatic variables for row and column are defined in the for loops, only one 
  128. value can be passed using a single parameter. In this case, that is the value 
  129. of <B>multi</B> as noted in the call statement, i.e. the address of the first element, often 
  130. referred to as a pointer to the array. Thus, the only way we have of informing 
  131. the compiler of the 2nd dimension is by explicitly including it in the parameter definition.
  132. <P>
  133.  
  134.     In fact, in general all dimensions of higher order than one
  135. are needed when dealing with multi-dimensional arrays.  That is
  136. if we are talking about 3 dimensional arrays, the 2nd <B>and</B> 3rd
  137. dimension must be specified in the parameter definition.
  138.  
  139. <DL>
  140.  
  141. <DT><Center><A HREF=ch8x.htm>Continue with Pointer Tutorial</A></Center>
  142.  
  143. </DT></P>
  144.  
  145. <DT><Center><A Href=pointers.htm>Back to Table of Contents</Center></A><P>
  146.  
  147. </DL>
  148.  
  149.