# Pointers to Arrays and Arrays of Pointers

#### Array Pointers

Pointers occur in many C programs as references to arrays , and also as elements of arrays. A pointer to an array type is called an array pointer for short, and an array whose elements are pointers is called a pointer array.

For the sake of example, the following description deals with an array of int. The same principles apply for any other array type, including multidimensional arrays.

To declare a pointer to an array type, you must use parentheses, as the following example illustrates:

```int (* arrPtr) = NULL; // A pointer to an array of ten elements with type int.
```

Without the parentheses, the declaration int * arrPtr; would define arrPtr as an array of 10 pointers to int. Arrays of pointers are described in the next section.

In the example, the pointer to an array of 10 int elements is initialized with NULL. However, if we assign it the address of an appropriate array, then the expression *arrPtr yields the array, and (*arrPtr)[i] yields the array element with the index i. According to the rules for the subscript operator, the expression (*arrPtr)[i] is equivalent to *((*arrPtr)+i). Hence **arrPtr yields the first element of the array, with the index 0.

In order to demonstrate a few operations with the array pointer arrPtr, the following example uses it to address some elements of a two-dimensional arraythat is, some rows of a matrix:

```// Array of three rows, each with 10 columns. The array name is a pointer to the first element; i.e., the first row.
int matrix;

// Let arrPtr point to the first row of the matrix.
arrPtr = matrix;

// Assign the value 5 to the first element of the first row.
(*arrPtr) = 5;

//Assign the value 6 to the last element of the last row.
arrPtr = 6;

// Advance the pointer to the next row.
++arrPtr;

// Assign the value 7 to the first element of the second row.
(*arrPtr) = 7;
```

After the initial assignment, arrPtr points to the first row of the matrix, just as the array name matrix does. At this point you can use arrPtr in the same way as matrix to access the elements. For example, the assignment (*arrPtr) = 5 is equivalent to arrPtr = 5 or matrix = 5.

However, unlike the array name matrix, the pointer name arrPtr does not represent a constant address, as the operation ++arrPtr shows. The increment operation increases the address stored in an array pointer by the size of one arrayin this case, one row of the matrix, or ten times the number of bytes in an int element.

```arrPtr = a;    // Error: mismatched pointer types.
```

The reason is that an array name, such as a, is implicitly converted into a pointer to the array’s first element, not a pointer to the whole array. The pointer to int is not implicitly converted into a pointer to an array of int. The assignment in the example requires an explicit type conversion, specifying the target type int (*) in the cast operator:

```arrPtr = (int (*))a;     // OK
```

#### Pointer Arrays

Pointer arraysthat is, arrays whose elements have a pointer typeare often a handy alternative to two-dimensional arrays. Usually the pointers in such an array point to dynamically allocated memory blocks.

For example, if you need to process strings, you could store them in a two-dimensional array whose row size is large enough to hold the longest string that can occur:

```#define ARRAY_LEN 100
#define STRLEN_MAX 256
char myStrings[ARRAY_LEN][STRLEN_MAX] =
{ // Several corollaries of Murphy's Law:
"If anything can go wrong, it will.",
"Nothing is foolproof, because fools are so ingenious.",
"Every solution breeds new problems." };
```

However, this technique wastes memory, as only a small fraction of the 25,600 bytes devoted to the array is actually used. For one thing, a short string leaves most of a row empty; for another, memory is reserved for whole rows that may never be used. A simple solution in such cases is to use an array of pointers that reference the objectsin this case, the stringsand to allocate memory only for the pointer array and for objects that actually exist. Unused array elements are null pointers.

```#define ARRAY_LEN 100
char *myStrPtr[ARRAY_LEN] =    // Array of pointers to char
{ // Several corollaries of Murphy's Law:
"If anything can go wrong, it will.",
"Nothing is foolproof, because fools are so ingenious.",
"Every solution breeds new problems." };
```
Share