Sams Teach Yourself C in 21 Days

(singke) #1
Pointers: Beyond the Basics 389

15


How can you use pointers to pointers? The most common use involves arrays of pointers,
which are covered later in today’s lesson. Listing 19.5 on Day 19, “Exploring the C
Function Library,” presents an example of using multiple indirection.

Pointers and Multidimensional Arrays ..............................................................


Day 8, “Using Numeric Arrays,” covers the special relationship between pointers and
arrays. Specifically, the name of an array without its following brackets is a pointer to
the first element of the array. As a result, it’s easier to use pointer notation when you’re
accessing certain types of arrays. These earlier examples, however, were limited to sin-
gle-dimensional arrays. What about multidimensional arrays?
Remember that a multidimensional array is declared with one set of brackets for each
dimension. For example, the following statement declares a two-dimensional array that
contains eight type intvariables:
int multi[2][4];
You can visualize an array as having a row and column structure—in this case, two rows
and four columns. There’s another way to visualize a multidimensional array, however,
and this way is closer to the manner in which C actually handles arrays. You can consider
multito be a two-element array, with each of these two elements being an array of four
integers.
In case this isn’t clear to you, Figure 15.2 dissects the array declaration statement into its
component parts.
Here’s how to interpret the components of the declaration:


  1. Declare an array named multi.


FIGURE15.1
A pointer to a pointer.

1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015

12

1000

1006

x

ptr

ptr_to_ptr

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

Free download pdf