Expert C Programming

(Jeff_L) #1

What "Multidimensional" Means in Different Languages


The Ada standard explicitly says arrays of arrays and multidimensional arrays are different.


The Pascal standard explicitly says arrays of arrays and multidimensional arrays are the
same.


C only has what other languages call arrays of arrays, but it also calls these
"multidimensional."


C's approach is somewhat unique: The only way to define and reference multidimensional arrays is
with arrays of arrays. Although C refers to arrays of arrays as multidimensional arrays, there is no way
to "fold" several indices such as [i][j][k] into one Pascal-style subscript expression such as
[i,j,k]. If you know what you're doing, and you don't mind a nonconforming program, you can
calculate what the equivalent offset to [i][j][k] is, and just reference with one subscript [z]. This
is not recommended practice. Worse still, [i,j,z] is a legal (comma-separated) expression. It
doesn't reference several subscripts, though. C thus supports what other languages generally call
"arrays of arrays," but C blurs the distinction and confuses the hell out of many people by also calling
these "multidimensional arrays." (See Figure 9-7.)


Figure 9-7. Arrays of Arrays

Although its terminology refers to "multidimensional arrays," C really only supports "arrays of
arrays." It greatly simplifies this rather taxing area if your mental model is that whenever you see
"array" in C, think "vector" (i.e., one-dimensional array of something, possibly another array).


Handy Heuristic

Free download pdf