Pointer Exercises

 

  1. Write a type definition for dynamically declared arrays whose elements are of type char.  Call the new type charArray.  For example:  the new type might appear in our code as follows:  charArray myStr = new char[50];

  2. Given the statement in the example above, write a section of code which fills myStr with characters which the user types in at the keyboard (including spaces and other white space), until the user types <ENTER>.

  3. Write the statement that returns the array allocated in exercise 1 to the free store.

  4. What is the output of the following code:

    int ARRAY_SIZE = 10;

    int *a;

    a = new int[ARRAY_SIZE];

    for (int i = 0; i < ARRAY_SIZE; i++) {

        *(a + i) = ARRAY_SIZE - i;

    }

    for (int j = 0; j < ARRAY_SIZE; j++) {

        cout << a[j] << " ";

    }

    cout << endl;

     

  5. Write a class definition for an object called twoDimPoint which has attributes x and y which are of type double.

  6. Do the following:

    a.  Declare a variable pt of type twoDimPoint and assign its x attribute to 4.0 and its y attribute to 7.0.

    b.  Declare a variable ptPointer which is a pointer to a twoDimPoint object and cause it to point to (or reference) pt.

    c.  Change pt's x attribute to 8.0 using ptPointer.

  7. Write a type definition called funcPtr for the type:  a pointer to a function where the function has two reference parameters to ints and returns a bool.

  8. Write a program which reads each character (including white space) of a file, changes each lower-case letter in the file to upper case, and then writes the (possibly changed) character to a new file.  The filenames should be specified via command-line arguments.