An Array of Pointers to Pointers


The supercharger of pointers is an array of pointers to pointers because an array of pointers to pointers enables you to reorganize tons of data in memory by simply referring to memory addresses. You were introduced to arrays of pointers to pointers at the beginning of this chapter. You ll now learn the ins and outs of using them.

Be forewarned: an array of pointers to pointers is one of the most abstract concepts to grasp in programming. Therefore, it is critical that you draw a picture of computer memory as you analyze a program that uses an array of pointers to pointers; otherwise , you are bound to become unnecessarily frustrated.

Let s begin by recalling the basics. It probably seems that you read these terms countless times in the last chapter and this chapter, but these terms are so important to understanding an array of pointers to pointers that we ll talk about them one more time.

A variable is a reference to a memory location used to store data that is described in a data type. A pointer variable is the same as a variable except its contents are the memory address of another variable. A pointer to a pointer , which you learned about in Chapter 2, is a pointer variable. The contents of the pointer variable is a memory address of another pointer variable.

More on an Array of Pointers to Pointers

Before we show you how to use an array of pointers to pointers, let s be sure that you understand how arrays, arrays of pointers, and arrays of pointers to pointers join forces to rearrange tons of data efficiently .

Think of an array as the storage place of the tons of data. The last thing you want to do is to physically reorganize a lot of data in computer memory because it is inefficient.

Think of an array of pointers as the storage place for memory addresses of data stored in an array. This is like a notepad where you jot down memory addresses of data.

Think of an array of pointers to pointers as the place where you reorganize the data contained in the array by indirectly rearranging memory addresses contained in the array of pointers. You can have any number of arrays of pointers to pointers, each indirectly ordering the content of the array of pointers in a different order.

Let s say that you have a list of three names, as shown in Figure 3-10. Each name is assigned to elements of an array in reverse alphabetical order. You can reorder those names without changing their order in the array by using an array of pointers and an array of pointers to pointers.

click to expand
Figure 3-10: An array of pointers to pointers reorganizes names without changing the order of the array that contains the names.

In Figure 3-10, elements of the array of pointers are assigned the memory addresses of each element of the array. Elements of the array of pointers to pointers are assigned the memory addresses of elements of the array of pointers. Notice that these memory addresses are assigned in the reverse order that they appear in the array of pointers, which indirectly references the array of names in reverse order ”that is, in alphabetical order.

Declaring and Using an Array of Pointers to Pointers

An array of pointers to pointers is declared nearly the same way as you declare an array of pointers, except two asterisks (**) are used before the name of the array, as shown here:

 string **ptPTStudents[3]; 

The data type of an array of pointers to pointers is used a little differently than the data type of an array of pointers. Previously in this chapter, you learned that the data type of an array of pointers refers to the data type stored at memory addresses that are assigned to elements of the pointer array.

Suppose you declared an array of strings and an array of pointers to strings and then assigned memory addresses of the string array to the pointer array. The data type of the pointer array is a string data type, which tells the computer that elements of the pointer array contain memory addresses of strings.

The data type of the array of pointers to pointers corresponds to the data type of the pointers that are assigned to elements of the array of pointers to pointers. Let s say you assign elements of the pointer array to elements of the array of pointers to pointers. Because the pointer array points to strings, the array of pointers to pointers must also use the string data type.

The data type of an array of pointers to pointers tells the computer that the memory address contains a memory address that is a pointer. This pointer contains a memory address of a string or whatever the data type specified when the pointer is declared.

Assigning Values to Elements of an  Array  of  Pointers  to  Pointers

You assign a value to an element of an array of pointers to pointers the same way you assign a value to an element of an array of pointers. That is, use the address operator (&) to reference either a pointer variable or an element of an array of pointers, as shown here:

 ptPTStudents[0] = &ptStudents[0]; 

You ll recall that the address operator tells the computer to assign the memory address of a pointer variable or array element to the element of the array of pointers to pointers, not the value stored at that address. The previous example assigns the memory address of a pointer array to the element of the array of pointers to pointers. It does not assign the contents of the ptStudents[0] , which is also a memory address.

Using the Contents of an Array of Pointers to Pointers

Accessing the contents of an element of an array of pointers to pointers is nearly identical to the way the content of an element of a pointer array is accessed. Previously, you learned that you dereference an element of a pointer array when you want to tell the computer to use the content of the element, as shown here:

 cout << *ptStudents[0] << endl; 

An element of an array of pointers to pointers is accessed by using two asterisks (**), as shown in this statement:

 cout << **ptPTStudents[0] << endl; 

Pointers to Pointers in Action

Now that you have a firm grasp on arrays and an array of pointers to pointers, we ll show you how you can harness the power of the array. Figure 3-10 is a diagram of how an array containing three names is reordered using a pointer array and an array of pointers to pointers. Figure 3-11 shows how to do this in C and C++. Remember that Java does not permit programmers to use pointers directly, but understanding how pointers work in C and C++ will help you understand how pointers are used in Java.

click to expand
Figure 3-11: Using a pointer array and an array of pointers to pointers to display the contents of an array of strings

The program begins by declaring an array of strings called students that is initialized with the names of three students. Notice the order of these names. The program will use an array of pointers to pointers to reverse this order.

Once the array of strings is declared, the program declares a pointer array and an array of pointers to pointers, both of which have three elements. Two integers are then declared and used as control variables for the for loop.

The first for loop displays elements of the students array to show the original order in which names are stored in the array, as shown in the left stack in Figure 3-10.

The second for loop assigns memory addresses of each element of the students array to elements of the pointer array, as shown in the center stack in Figure 3-10.

The third for loop uses the ptStudents array of pointers to display students contained in the students array. Names appear in the same order shown in the left stack in Figure 3-10.

The fourth for loop assigns the memory address of each element in the pointer array to elements of the array of pointers to pointers, which is called ptPtStudents . This is where the program reorders names of the students array. It may look confusing at first glance, but here s what is happening.

The i control variable is initialized to 0, and the x control variable is initialized to 2. These determine the starting points in the array of pointers to pointers and the pointer array. The ptPtStudents pointer to pointer array begins with the first element, while the ptStudents array pointer begins with the last element. This is because the program reverses the order in which names appear in the students array. The last name will appear first in the reordered list.

Each time the for loop is looped, the value of the i variable is incremented, causing the program to move to the second and third elements of the ptPtStudents array of pointers to pointers. At the same time, the value of the x variable is decremented, causing the ptStudents pointer array to move to the second and first elements, as shown in the right stack in Figure 3-10.

The fifth for loop steps through elements of the ptPtStudents array of pointers to pointers displaying the corresponding name in the students array on the screen.




Data Structures Demystified
Data Structures Demystified (Demystified)
ISBN: 0072253592
EAN: 2147483647
Year: 2006
Pages: 90

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net