Introducing an array of pointers to arrays
Before finishing this chapter, it is worth introducing the concept of an array of pointers to arrays. This may be thought of as an alternate 2D array. Such an array is somewhat different in memory than the standard arrays that we have so far encountered. Even though its memory representation is different, we access this alternate array in exactly the same way as we would a standard 2D array. Therefore, some caution is required when traversing the two kinds of arrays.
We declare a standard 2D array in the following way:
int arrayStd[3][5];
We have allocated a contiguous and inseparable block of 15 integers, which has 3 rows of 5 integers. Our conceptual memory picture of this is a single block referenced via a single name, arrayStd
, as follows:
Figure 14.5 – A standard, contiguous 2D array
To declare an alternate 2D array using arrays of pointers, we would do the following:
...