Computer Science 2611
Spring 2000
Laboratory Assignment 12 (last one)

Your solution to assignment 11 defines a constant such as MAX_NAME_LENGTH for the number of characters that a name can occupy, and another such as MAX_NUMBER_LENGTH for the maximum number of characters in a number. When a string is stored, the maximum number of bytes is used, regardless of how long the string is. Often, most of the space is wasted.

For example, suppose that MAX_NAME_LENGTH is chosen to be 100, to allow for long names. To store the name "Bob", your program uses 101 bytes. Only the first four of those bytes will have useful information in them; the remaining 97 bytes are wasted.

It is possible to reduce the waste, using only as many bytes as are needed for each name. Instead of storing an array in the phoneinfo structure, store pointer to the array. So the phoneinfo structure becomes as follows.

    struct phoneinfo {
      char* name;
      char* number;
    };
When you read a name, set aside an array to read it into. Allocate MAX_NAME_LENGTH+1 bytes for that array. Do a similar thing for the number. After reading the name and number into those arrays, allocate new arrays that are just large enough to hold the names. For example, if the name has been read into array nameArray, then say
    char* namep = new char[strlen(nameArray) + 1];
Now, using strcpy, copy the string that is in array nameArray into the array pointed to by namep. An important thing is that
A pointer to an array is treated in the same way as the array.
So treat namep exactly as if it is an array. To copy from array nameArray into the array that is now pointed to by namep, write
    strcpy(namep, nameArray);
Having copied the name into the array pointed to by namep, array nameArray can be reused to read the next name.

Make your program work with this new kind of structure, storing only as many bytes as are needed for each name and number. Turn in a printout of your program.