Expert C Programming

(Jeff_L) #1

When several "array of" specifications (i.e., the square brackets that denote an index) are adjacent, a
multidimensional array is declared.


...But Every Other Language Calls Them "Arrays of Arrays"

What these people mean is that C doesn't have multidimensional arrays as they appear in other
languages, like Pascal or Ada. In Ada you can declare a multidimensional array as shown in Figure 9-5.


Figure 9-5. Ada Example

But you can't mix apples and sauerkraut. In Ada, multidimensional arrays and arrays of arrays are two
completely different beasts.


The opposite approach was taken by Pascal, which regards arrays of arrays and multidimensional
arrays as completely interchangeable and equivalent at all times. In Pascal you can declare and access
a multidimensional array as shown in Figure 9-6.


Figure 9-6. Pascal Example

The Pascal User Manual and Report [1] explicitly says that arrays of arrays are equivalent to and
interchangeable with multidimensional arrays. The Ada language is more tightly buttoned, and strictly
maintains the distinction between the arrays of arrays and multidimensional arrays. In memory they
look the same, but it makes a big difference as to what types are compatible and can be assigned to
individual rows of an array of arrays. It's rather like choosing between a float and an int for a variable:
you choose the type that most closely reflects the underlying data. In Ada, you would typically use a
multidimensional array when you have independently varying indices, such as the Cartesian
coordinates specifying a point. You would typically use an array of arrays when the data is more
hierarchical, for example, an array [12] months of [5] weeks of [7] days for keeping a daily record of
something, but also being able to manipulate an entire week or month at a time.


[1] The Pascal User Manual and Report, Springer-Verlag, 1975, p. 39.


Handy Heuristic

Free download pdf