7.5 ARRAYS OF POINTERS IN C


7.5 ARRAYS OF POINTERS IN C++

Here are a couple of examples of declarations of arrays of pointers in C++:

     int* int_array[1000];                  // an array of 1000 pointers                                            // each pointing to an int     string* names[100];                    // an array of 100 pointers, each                                            // pointing to a string type 

Arrays of pointers are extremely useful for C++ programming. To illustrate, let's say that we expect a maximum of 100 students to enroll in a class. To hold the names of these students we create an array by a declaration such as

       const int MaxClassSize = 100;       string* names[ MaxClassSize ]; 

Before allowing the students to register for a class, we could initialize this array by

       for ( int i = 0; i < MaxClassSize; i++ )        names[ i ] = 0; 

or, using the array initialization syntax we discuss later in this chapter, even more simply by

      string* names[100] = {0}; 

Now as a student enrolls, his or her name could be added to the list by astatement like

      names[i] = new string( "Zaphod" ); 

And if, after registering, this student chooses to drop the course, we could take care of that situation by resetting

      names[i] = 0; 

If at some point during this registration process, we wanted to print out the list of all the names currently enrolled, we could use a loop like

      for ( int i = 0; i < MaxClassSize; i++ )        if ( names[ i ] != 0 )           cout << *names[ i ] << endl; 

The advantage of this approach is that the test on the ith pointer in thearray

       if ( names[ i ] != 0 ) ... 

tells us right away whether or not the ith element in the array currently belongs to a registered student. If we did not use this test and simply used an ostensibly simpler loop like

      for ( int i = 0; i < MaxClassSize; i++ )          cout << *names[ i ]; 

you could get a hard-to-decipher run-time error because you could be trying to reach a memory location that was not yet appropriated, let alone properly initialized, or probably deallocated when a student dropped the course. Testing for whether or not we have a non-null pointer in an array of pointers is a very convenient way to check whether or not an object has yet been created.

An alternative to using a array of pointers would be to declare directly an array of strings, as opposed to an array of pointers to strings, by

      string names[ MaxClassSize ]; 

Now suppose we try to print out the names of the students who have enrolled so far by

      for ( int i = 0; i < MaxClassSize; i++ )        cout << names[ i ] << endl; 

This would work as expected if the entire array is full—that is, if all the 100 students in this example have registered. But if suppose the array is not yet full, this loop would give us a run-time error as we'd be trying to read memory locations that have not be yet been initialized. You see, we no longer have the luxury of using a test like

       if ( names[ i ] != 0 ) ...               // WRONG NOW 

because comparing a string to 0 makes no sense to the compiler, especially if that element of the array has not yet been created. Some readers might say that before allowing any students to register, why not initialize the entire array by using empty string literals:

       for ( int i = 0; i < MaxClassSize; i++ )         names[ i ] = ""; 

And then we could print out the entire array by using a construction similar to what was used for the pointer case:

       for ( i = 0; i < MaxClassSize; i++ )        if ( names[ i ] != "" )          cout << rnames[ i ] << endl; 

This would certainly work for the case of strings, because an empty string literal is something the compiler understands. But an empty something may not be defined for other class types. For example, for the class User that we have used in previous examples, it would be difficult to define the notion of an empty User.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

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