Data Structures Demystified (Demystified)
Previously, you learned that pointers are the backbone of data structures. Some programmers feel that an array of pointers, also known as a pointer array, is the backbone of pointers. An array of pointers is an array whose elements are pointers. That is, the value of each array element is a memory address similar to a pointer variable, which you learned about in Chapter 2.
The benefit of using an array of pointers instead of several pointer variables is that you can use a for loop to step through each element of the array to access memory addresses that are assigned to the array. You ll need to do this to efficiently access and reorder values stored in memory.
An array of pointers is not available in all programming languages. For example, Java doesn t let programmers use pointers, so you won t be able to create an array of pointers in Java. However, you can create an array of pointers in C and C++.
Note | It is technically incorrect to say that Java doesn t use pointers. Java does use pointers, but a programmer doesn t explicitly declare them. You can declare an array whose data type is a Java object, and this is in fact an array of pointers. The value of each array element is an object. When you switch those values to other array elements, you are moving memory addresses and not the object itself. |
An array of pointers is declared using nearly the same format as declaring an array of data types, with one exception. The name of the array must be preceded with an asterisk, as shown here:
char *grades[10];
The asterisk tells the computer that the array is a pointer array where each element can contain a memory address. The data type in this declaration statement tells the computer that memory addresses stored in array elements are memory addresses that contain a char value. This is the same pointer variable concept you learned in Chapter 2.
As you probably recall from your programming course, a computer copies the value of a variable in an assignment statement, as shown in the next example. The first two statements in this example reserve a memory location large enough to store a char and associate those memory addresses with the names finalGrade and recordedGrade . The first statement also stores the value A in memory. The last statement copies the value stored in the memory location represented by finalGrade to the memory address represented by the recordedGrade .
char finalGrade = 'A'; char recordedGrade; recordedGrade = finalGrade;
You assign a memory address of a variable to an element of an array of pointers by placing the address operator (&) in front of a variable name in an assignment statement to reference the variable. The ampersand (&) returns a pointer, and an asterisk (*) dereferences the pointer and tells the computer that you want the value pointed to by the pointer.
Referencing tells the computer to copy the memory address of the variable instead of copying the value stored in the memory address. This is illustrated in the next example where the address of the finalGrade variable is referenced, resulting in the memory address of the finalGrade variable being assigned to the first element of the ptRecordedGrades array. The ptRecordedGrades is an array of pointers.
char finalGrade = 'A'; char *ptRecordedGrades[10]; ptRecordedGrades[0] = &finalGrade;
Programmers use an array of pointers in two ways: they use the address assigned to array elements, and they use the content of the memory address assigned to an array element. Let s take a look at how to use addresses stored in an array of pointers.
The following example initializes three variables with grades and declares two pointer arrays called ptGradeBook and ptRecordedGrade . The addresses of the three variables are then assigned to each element of the ptGradeBook pointer array. A for loop then copies memory addresses stored in the ptGradeBook array to the ptRecordedGrade array. Notice that the ampersand is not used in this assignment expression because we want the content of each array element to be copied to the ptRecordedGrade array.
char bobGrade = 'A'; char maryGrade = 'B'; char amberGrade = 'A'; char *ptGradeBook[3]; char *ptRecordedGrade[3]; ptGradeBook [0] = &bobGrade; ptGradeBook [1] = &maryGrade; ptGradeBook [2] = &amberGrade; for (int i = 0; i < 3; i++) ptRecordedGrade[i] = ptGradeBook[i];
Now we ll modify this program slightly in the next example by changing the ptRecordedGrade array from an array of pointers to an array of integers. We ll then use the for loop to copy the contents of the variables to the recordedGrade by using the pointer array, as shown here:
char bobGrade = 'A'; char maryGrade = 'B'; char amberGrade = 'A'; char *ptGradeBook[3]; char recordedGrade[3]; ptGradeBook [0] = &bobGrade; ptGradeBook [1] = &maryGrade; ptGradeBook [2] = &amberGrade; for (int i = 0; i < 3; i++) recordedGrade[i] = *ptGradeBook[i];
The last statement in this example dereferences each element of the ptGradeBook array by preceding the name of the array with an asterisk. This tells the computer to first go to the memory address stored in the array element and then copy the value stored at the memory address to the element of the recordedGrade array element (see Figure 3-9).
Категории