Learn something new every day
More Info... by email
An array of pointers is a standard array data type that holds a sequence of memory addressed to data types as opposed to holding other scalar types such as integers or floating point numbers. By using an array of pointers, a program is able to have an array of nearly any data type, data structure, class or even function. Efficient sorting of an array of pointers also is possible so that, if desired, the addresses can be aligned according to their physical location in computer memory. Some of the complications in using an array of pointers are that the data can be easily corrupted and that the type of data being pointed to needs to be managed internally to prevent memory overflow problems.
Not all programming languages allow an array of pointers as the concept was originally conceived. High-level languages that automate memory management do not use pointers in the same way as lower-level languages, although technically an array of class objects in a high level, object-oriented language is an array of pointers; even so, they cannot be used or manipulated in the same way. In a low-level language, the array will actually hold an integer value that is the direct address of the pointer in the memory heap. In most high-level languages, this functionality is not possible, although alternate paradigms exist.
One of the aspects that makes an array of pointers valuable as a programming tool is that the pointers can reference any type of data available. The pointer is a data type in itself, even though its only purpose is to point toward another data type. This means an array of pointers can actually contain a variety of data types in the same array, although the program would need to have a mechanism to keep track of what each pointer was referencing.
A powerful aspect of pointers is that they can actually point to compiled program code in memory. This allows an array to point to functions, not just data. An array of functions can allow dynamic numerical referencing of code. This can be useful when programming a language interpreter or other type of software that needs to call certain functions based on unpredictable input without the need for large conditional statements such as switch blocks.
Some problems presented by an array of pointer data are the possibility of losing the address, preventing that memory from being freed later, and the chance that an address can be corrupted through an unintentional operation such as addition. Attempts to access an incorrect address could lead to corrupted data being read or, in extreme cases such as a failed initialization, a segmentation fault that could cause the program to terminate unexpectedly. Cautious programming can help to mitigate the possibility of these events.