Expert C Programming

(Jeff_L) #1

dimensional array of pointers is that looking at the reference squash[i][j] does not tell
you whether squash was declared as:


int squash[23][12]; /* array [23] of array[12] of int


*/


or


int squash[23]; / an Iliffe vector of 23


pointers-to-int */


or


int * squash; / pointer to a pointer to int */


or even


int ( squash)[12]; / pointer to array-of-12-ints */


This is similar to the inability (inside a function) to tell whether an array or a pointer was
passed as the actual argument, of course, for the same reason: array names as l-values
"decay" into pointers.


The identical reference squash[i][j] works for accessing any of these definitions,
though the kind of access is different in the different cases.


Just as with an array of arrays, an individual character in an Iliffe vector will be referenced with two
indices, (e.g., pea[i][j]). The rules for pointer subscripting (see Chapter 9, page 242) tell us that
pea[i][j] becomes:


( (pea + i) + j )


Does this look familiar? It should. It's exactly the same expression to which a multidimensional array
reference decomposes, and it is explained this way in many C books. There's one big problem,
however. Although the two subscripts look identical in the source, and both decompose to the
identical pointer expression, a different kind of reference is done in each case. Tables 10-1 and 10-2 show
the distinction.


Table 10-1. char a[4][6]—An Array-of-Arrays
char a[4][6] —An Array-of-Arrays
Free download pdf