Expert C Programming

(Jeff_L) #1

the declaration). Think of it as parenthesized—(char *) turnip[23]. It is not what it
appears to be if you read it from left to right: a pointer to a 23-element array of characters.
This is because index brackets have a higher precedence than the pointer asterisk. This is
fully covered in Chapter 3 on declarations.


An array of pointers that implements support for a multidimensional array is variously termed an
"Iliffe vector," a "display," or a "dope vector." A display is also used in Britain to mean a vector of
pointers to active stack frames of lexically enclosing procedures (as an alternative to a static link
followed by a linked list). An array of pointers like this is a powerful programming technique that has
wide applicability outside C. In diagram form, we have the structure shown in Figure 10-3.


Figure 10-3. An Array of Pointers-to-String

The array must be initialized with pointers to memory allocated for the strings, either at compile time
with a literal initializer, or at runtime with code like this:


for (j=0;j<=4;j++) {


pea[j] = malloc( 6 );


Another approach is to malloc the entire x by y data array at once,


malloc(row_size column_size sizeof(char) ) ;


then use a loop to assign pointers to locations within that region. The


entire array is guaranteed to be stored in contiguous memory in the


order that C allocates static arrays. It reduces the bookkeeping


overhead of calls to malloc, but prevents you from freeing an


individual string when you're done with it.


Software Dogma


When You See squash[i][j] —You Don't Know How It Was Declared!


One problem with being able to double-subscript a two-dimensional array and a one-

Free download pdf