C Language | Pointers | Arrays of Pointers

This article explains how to create an array whose elements are pointers. Arrays of pointers help manage large amounts of data in a structured way.

Creating an Array of Pointers

A pointer is a type of variable that stores a memory address. Like ordinary integer variables, pointers can be stored in arrays. Except that each element is a pointer, an array of pointers works like an ordinary array. The same principles apply to one-dimensional and multidimensional arrays.

Declare an array of pointers as follows.

Declaring an Array of Pointers

type *pointerVariableName[];

This syntax lets you manage multiple pointers as an array. Specify an index to retrieve a stored address or access the content at that address. If you understand arrays and pointers, an array of pointer variables is straightforward to use.

For example, when managing multiple strings like an array, an array of pointers to strings is often more natural than a multidimensional array.

Code 1

#include <stdio.h>

int main() {
 char *chStr[] = {
   "Blue Blue Glass Moon" ,
    "Under The Crimson Air"
 };

  printf("%s\n%s\n" , *chStr , *(chStr + 1));
 return 0;
}

The chStr array in Code 1 has two elements that store pointers to strings. The elements do not contain the strings themselves. They store only pointers to the strings. Compared with a string table implemented as a two-dimensional array, this representation is flexible because you can update the table simply by changing the referenced addresses.