Sams Teach Yourself C in 21 Days

(singke) #1

  1. The array multicontains two elements.

  2. Each of these two elements contains four elements.

  3. Each of the four elements is of type int.
    You read a multidimensional array declaration starting with the array name and moving
    to the right, one set of brackets at a time. When the last set of brackets (the last dimen-
    sion) has been read, you jump to the beginning of the declaration to determine the array’s
    basic data type.
    Under the array-of-arrays scheme, you can visualize a multidimensional array as shown
    in Figure 15.3.


390 Day 15

FIGURE15.2
The components of a
multidimensional array
declaration.

int multi[2][4];

4 1 23

FIGURE15.3
A two-dimensional
array can be visualized
as an array of arrays.

multi multi[0]

multi[1][3]

Now, let’s get back to the topic of array names as pointers. (Today’s lesson is about
pointers, after all!) As with a one-dimensional array, the name of a multidimensional
array is a pointer to the first array element. Continuing with our example,multiis a
pointer to the first element of the two-dimensional array that was declared as int
multi[2][4]. What exactly is the first element of multi? It isn’t the type intvariable
multi[0][0], as you might think. Remember that multiis an array of arrays, so its first
element is multi[0], which is an array of four type intvariables (one of the two such
arrays contained in multi).
Now, if multi[0]is also an array, does it point to anything? Yes, indeed! multi[0]
points to its first element,multi[0][0]. You might wonder why multi[0]is a pointer.

25 448201x-CH15 8/13/02 11:13 AM Page 390

Free download pdf