How to Build an Array of Pointers in C Programming

By Dan Gookin

An array of pointers would be an array that holds memory locations. Such a construction is often necessary in the C programming language. Remember that an array of pointers is really an array of strings, shown in Crazy Pointer Arrays. That makes topic digestion easier.

CRAZY POINTER ARRAYS

#include <stdio.h>
int main()
{
 char *fruit[] = {
 "watermelon",
 "banana",
 "pear",
 "apple",
 "coconut",
 "grape",
 "blueberry"
 };
 int x;
 for(x=0;x<7;x++)
 puts(fruit[x]);
 return(0);
}

An array of pointers is declared in Crazy Pointer Arrays. It works similarly to an array of strings, although in this construction you don’t need to specifically count individual string lengths. That’s because the array is really an array of pointers, or memory locations. Each string dwells somewhere in memory. The array simply lists where each one starts.

Exercise 1: Type the source code from Crazy Pointer Arrays into your editor. Build and run to confirm that it works.

Which part of Crazy Pointer Arrays do you think could be improved?

Exercise 2: Using information below as your guide, replace the array notation at Line 17 in Crazy Pointer Arrays with pointer notation.

Array alpha[] Pointer a
alpha[0] *a
alpha[1] *(a+1)
alpha[2] *(a+2)
alpha[3] *(a+3)
alpha[n] *(a+n)

The reason that your solution to Exercise 2 works (assuming that you got it correct) is that the fruit array contains pointers. The value of each element is another pointer. But that’s nothing; consider Pointers-to-Pointers Example.

POINTERS-TO-POINTERS EXAMPLE

#include <stdio.h>
int main()
{
 char *fruit[] = {
 "watermelon",
 "banana",
 "pear",
 "apple",
 "coconut",
 "grape",
 "blueberry"
 };
 int x;
 for(x=0;x<7;x++)
 {
 putchar(**(fruit+x));
 putchar('n');
 }
 return(0);
}

Line 18 in Pointers-to-Pointers Example contains the dreaded, feared, avoided, and cursed ** notation, or double-pointer notation.

Exercise 3: Carefully type the source code from Pointers-to-Pointers Example into your editor. Compile and run.

To understand the **(fruit+x) construct, you must work from the inside out:

fruit+x

Variable fruit contains a memory address. It’s a pointer! The x is a value incrementing by one unit. In this case, the unit is an address because all elements of the fruit array are pointers.

*(fruit+x)

You’ve seen the preceding construction already. It’s the contents of the address fruit+x. From the code, fruit is an array of pointers. So the result of the preceding operation is . . . a pointer!

**(fruit+x)

Finally, you get a pointer to a pointer or — put better — a peeker to a peeker. If the inside peeker is a memory address, the outside peeker (the first asterisk) is the content of that memory address.

image0.jpg

It helps to remember that the ** operator is almost always (but not exclusively) tied to an array of pointers; or, if you want to make it simple, to an array of strings. The first column is the address of an array of pointers, the second column is the pointer itself (a string), and the column on the right is the first character of the string.

If you’re still confused, consider mulling over pointer notation and array notation:

Pointer Notation Array Notation Description
**ptr *array[] Declares an array of pointers.
*ptr array[0] The address of the first pointer in the array; for a string
array, the first string.
*(ptr+0) array[0] The same as the preceding entry.
**ptr array[0][0] The first element of the first pointer in the array; the first
character of the first string in the array.
**(ptr+1) array[1][0] The first element of the second pointer in the array; the first
character of the second string.
*(*(ptr+1)) array[1][0] The same as the preceding entry.
*(*(ptr+a)+b) array[a][b] Element b of pointer a.
**(ptr+a)+b array[a][0]+b This item isn’t really what you want. What this item
represents is the value of element 0 at pointer a plus the
value of variable b. Use the *(*(ptr+a)+b) notation
instead.

In the table, pointer notation (using variable ptr) is compared with the equivalent array notation (using variable array).

Exercise 4: Rework your source code from Exercise 3 so that each individual character in a string is displayed, one at a time, by using the putchar() function. If you can write the entire putchar() operation as a while loop’s condition, you really understand the material.