Arrays of Cstrings

  1. Since cstrings are stored in single dimensional character arrays, an array of cstrings is represented as a 2D character array.

  2. For example the following declares and initializes such an array of cstrings:
    
    char scientists[4][10] = {"Newton", 
                              "Maxwell", 
                              "Einstein", 
                              "Feynman"};
    
    Note that the above array can hold a list of up to 4 cstrings (representing here the names of scientists), and each cstring can have up to 9 characters.

  3. An array of cstrings can be manipulated by using both indexes simultaneously like any other two-dimensional arrays.

  4. It is better to manipulate it by a loop that steps through the first index and treats each indexed variable as a single cstring variable that is manipulated by some cstring function.

  5. An example (and a bit of gossip) is shown below:

    #include <iostream>
    using namespace std;

    int main ( )
    {
        char scientists[4][10];
        cout << "Enter the names of 4 famous scientists.\n";
        for (int n = 0; n < 4; n++)
        {
            cout << "This scientist married " << n << " times: ";
            cin.getline(scientists[n],10);
            // since the names contain no whitespaces,
            // we can also use:
            // cin >> scientists[n];
            cout << "Are you sure " << scientists[n] << " married "
                            << n << " times?\n";
        }
        return 0;
    }