Page 81 - PowerPoint Presentation
P. 81

CAVITE STATE UNIVERSITY
                               T3 CAMPUS
                               Department of Information Technology      DCIT 25 – Data Structures and Algorithms

                       An Array of Pointers is nearly identical to a pointer variable except that each array
               element contains a memory address. Assign the memory address of each element of the
               letters array to element of the ptLetters array, which is an array of pointers. Here’s how this
               is done in C++:
                                         char *ptLetters (3);
                                         for (int i=0; i<3; i++) {
                                                ptLetters[i] = &letters[i];
                                         }

                       Remember also the ampersand (&) in last chapter, which is called the address operator
               that tells the computer to assign the memory address of the element of the letters array and
               not the contents of the element.
                       The final step is to create an array of pointers to pointers and then use it to change the
               order of the letters array when printing the letters array on the screen. A pointer to a pointer
               is a variable that contains the address pointer. In the example, you use an array of pointers to
               a pointer where each element of the array is like a pointer to a pointer variable. That is, each
               element is assigned an address of a pointer.
                       Use the following code to assign the memory address of each element of the ptLetters
               pointer array to the ptPtLetters pointer to pointer array. Notice that you don’t use a for loop.
               This is because you need to change the order of the letters array without changing the letters
               array itself. If you printed elements of the ptPtLetters array, what would be displayed on the
               screen?
                                         char **ptPTLetters[3];
                                         ptPtLetters[0] = &ptLetters[2];
                                         ptPtLetters[1] = &ptLetters[1];
                                         ptPtLetters[3] = &ptLetters[0];

                       Here is the code that prints the ptPtLetters array:
                                         for (int i=0; i<3; i++) {
                                                cout<< **ptPtLetters[i] <<endl;
                                         }

                       The answer to the question is A B C. Follow the figure above (Elements of an array
               stored sequentially in memory) as we explain how this works. The first element of the
               ptPtLetters array is located at memory address 10. The content of memory address 10 is 8,
               which is memory address 8 because memory address 10 is the last element of array
               ptLetters – a pointer. The value of memory address 8 is 3, which is the memory address of
               the third element of the array letters.
                              When the computer sees the ptPtLetters[i] statement for the first time, it goes
               to the array element ptPtLetters[0] and reads its value, which is 8. The computer then goes
               to memory address 8 and reads its content because memory address 8 is a pointer. The
               content of memory address 8 is 3, which is the memory address of the third element of the
               letters array. The computer reads the content of memory address 3 and displays the content
               on the screen.
                       This can be a bit tricky to follow unless you use the figure as a guide; you can also use
               the figure to explain how the computer displays the other letters.
                       The importance of using arrays for data structures is that you can easily change the
               order of data by using pointers and pointers to pointers without having to touch the original
               data.

               Declaring an Array
                       The way to declare an array depends on the programming language used to write your
               program. Let’s begin by declaring an array where memory is reserved when you compile your
               program. This technique is similar in Java, C, and C++, except in Java that you must initialize



                                                                                                 Page | 28
   76   77   78   79   80   81   82   83   84   85   86