Home Segments Index Top Previous Next

354: Sidetrip

Actually, when you declare an array parameter, you do not need to supply dimension information for the first dimension:

                               *-- First dimension's size omitted 
                               v 
double mean_djia (double array[ ][100], day) { 
  return 0.5 * (array[0][day] + array[1][day]); 
} 

To see why you can omit the first dimension's size, you need to know that the objects stored in two-dimensional arrays are placed sequentially in memory. In C, the storage is by rows:

  *-- [0, 0]          *-- [0, 99]                 *-- [2, 0] 
  |                   |                           | 
  v                   v                           v 
*---*---*---*--  ---*---*---*---*---*--  ---*---*---*---*---*---*-- 
|   |   |   |       |   |   |   |   |       |   |   |   |   |   | 
*---*---*---*---  --*---*---*---*---*---  --*---*---*---*---*---*--- 
                          ^                   ^ 
                          *-- [1, 0]          *-- [1, 99] 

You do not need to indicate the number of rows in the array-parameter declaration, because the C compiler needs to know only how many columns are stored in the array for each row to find the place in memory corresponding to any index pair.