Arrays of Pointers in C Plus Plus
Arrays of pointers in C Plus Plus
This tutorial introduces basic concepts and examples illustrating arrays of pointers in C Plus Plus. The topic is commonly referred to as double indirection by programmers.
Overview of the concept
A array in C Plus Plus as an ordered list of homogeneous data items. An array can be indexed by a non-negative integer value, starting at zero and proceeding to 1 less than the declared length of the array. Arrays are very common programming tools, but most arrays consist of simple data types such as integers, floats, or characters. Thus tutorial addresses arrays with an identical structure but comprised instead of memory addresses.
A memory address, commonly referred to as a pointer, describes a specific location in the physical or logical memory space (this tutorial does not discuss physical vs concepts). The structure of a memory address varies across operating systems and processors, but C Plus Plus is designed to be a portable language at the source code level, so all our examples should compile on most popular platforms.
A simple example in code
Consider the following code example:
// Introduction to arrays of pointers #include <iostream> using namespace std; // .Net code needs this, gcc not so much void main() { int *alpha; int myArray[10]; int *myArrayOfPointers[10]; }
Overview of the example code
Our example code demonstrates 4 different pointers.
- Line 7 declares a pointer to an integer.
- Line 8 also declares a pointer to an integer, but myArray is a pointer constant and cannot be overwritten.
- Line 9 declares an array of pointers to integers and a pointer constant called myArrayOfPointers.
This tutorial is primarily focused on the declaration in line 7. Each element of the array is a pointer to an integer. Contrast this line with line 6, where each element of the array is an integer. The difference is significant. An integer and a pointer to an integer are two completely different data types. The array called myArrayOfPointers still consists of 10 elements and can be indexed in the same manner as any other array, but care must be taken to manipulate the individual elements properly.
In the following code module we extend the code that was introduced above:
// Introduction to arrays of pointers #include <iostream> using namespace std; // .Net code needs this, gcc not so much void main() { int *alpha; int myArray[10]; int *myArrayOfPointers[10]; myArrayOfPointers[0] = 42; // probably won't compile myArrayOfPointers[0] = (int*)42; // a type cast saves the day }
Lines 11 and 12 were added to the previous code example. Line 11 illustrates a technique that worked very well in the original C language, but probably won't compile in C++ (C Plus Plus). The intention of line 11 is to initialize the first element of the array to the value 42. This value is assumed to be a memory address; since the array, myArrayOfPointers, is an array pointers to integers, anything we store in the array should be the memory address of an integer. We are assuming that our program has permission to access the information stored at memory address 42; that assumption cannot be validated until the program is compiled, linked, and executed, but the assumption is important to understand as the code is being analyzed.
If line 11 does not compile,and it probably will not, line 12 will solve the problem through the application of a type cast. The type cast is a syntax that tells the compiler to convert the integer 42 to an equivalent memory address. To many readers this type cast seems redundant because any programmer with embedded systems development experience recognizes the expression '42' as a memory address even before the application of the type cast. C Plus Plus is happy to convert the integer value 42 to a memory address format as long as the type cast syntax is present. Without the type cast, the compiler will generate at best a syntax warning and probably a syntax error.
Dereferencing the pointer in the array
Given that we have successfully declared an array of pointers to integers and we have assigned a value to one or more array elements, we can now apply the deference operator to make use of the pointer. The following code illustrates how to do this:
// Introduction to arrays of pointers #include <iostream> using namespace std; // .Net code needs this, gcc not so much void main() { int *alpha; int myArray[10]; int *myArrayOfPointers[10]; myArrayOfPointers[0] = 42; // probably won't compile myArrayOfPointers[0] = (int*)42; // a type cast saves the day *myArrayOfPointers[0] = 100; // Store 100 at address 42 cout << "\n" << *myArrayOfPointers[0]; // prints 100 }
Line 14 demonstrates the syntax for dereferencing the pointer stored in the array of pointers. We are storing the integer value 100 at memory address 42.The data currently stored at memory address 42 will be overwritten by this operation. The data currently stored at addresses 43, 44, and 45 will also be replaced, assuming 4 byte integers. If the target CPU uses 2 byte integers, then only data at addresses 42 and 43 will be overwritten. The format of the integer will be a function of the integer format required by the target CPU.
Line 15 prints the value stored at memory address 42. There is no format specifier in the cout statement. Note that the cout object 'knows' what data type to print based on the declaration in line 9.
Conclusion
Arrays of pointers can be used to directly manipulate memory. The syntax used to create and employ arrays of pointers is supported by C++ (C Plus Plus).