new, delete, and char** Exercises

Tom Kelliher, CS18

Apr. 26, 1996

Exercise

Write a program that:

  1. Uses new to allocate an array of three char*s.
  2. Reads three names (using getline()), one by one, into a char array ( buffer) from cin. Dynamically allocate space (pointed to by one of the char*s --- one for each name) for each name. Copy each name from buffer to its dynamically allocated space.
  3. Swap the first and third names (accomplished by merely swapping the pointers!)
  4. Print the names.
  5. Deallocate, using delete, all dynamically allocated memory (in the correct order).

Hand-in the program you write.

A few variables you'll need:

const int NAME_LEN = 80;
char** names;
char buffer[NAME_LEN];
char* temp;



Thomas P. Kelliher
Thu Apr 25 13:39:49 EDT 1996
Tom Kelliher