CS 31 Bridge – Summer 08                                                          Homework 8
 
Name: ______________________                     Group: _________________________________            Hours taken: ___
 
You can work in group of 2-3 people. You can talk and discuss among your group. However, write your own answers for the homework. Do NOT copy each other. Make your answers CLEAR and SHORT.
 
True False:

 

An explanation is required.

 

1.      A pointer is a variable that holds the address of some other location in memory.

 

2.      Pointer variables are just memory addresses and can be assigned to one another without regard to type.

 

3.      The declaration below declares three pointer variables of type pointer to double that is,  a pointer of type (double*)

double* p1, p2, p3;

 

4.      A pointer is an address, an address is an integer, but a pointer is not an integer.

 

5.      You can get a pointer value to initialize a pointer variable from an object of an appropriate type with the “address-of” operator, &.

 

6.      One can use the & operator to extract the value that a pointer points to.

 

7.      When declaring several pointer variables, there must be one pointer decelerator * for each pointer variable.

 

8.      There should eventually be a call to the operator delete on a pointer that points to the memory allocated by each call to new.

 

 

Multiple Choice:

 

There may be more than one correct answer. You must give all correct answers for full credit. An explanation is required.

 

1.      Some pointer arithmetic is allowed. Which of the following arithmetic operators is allowed?

a)      pointer + integer

b)      pointer - pointer

c)      pointer - integer

d)      integer + pointer

e)      integer * pointer

 

 

Free Form Questions:

 

1.      Suppose we have the following definitions and assignments:

double *p, v;

p = &v; // p gets the address of v

What is the effect of this assignment?

*p = 99.99

 

2.      Given the definitions,

int *p1, *p2;

p1 = new int;

p2 = new int;

 

You are to compare and contrast the two assignments.

a) p1 = p2;

b) *p1 = *p2;

 

 

 

 

3.      Given the declarations below, write a code fragment that allocates a nameless variable for pointer p1 to point to.

int *p1, *p2;

 

 

 

 

4.      Give the output from this code fragment:

int *p1, *p2;

p1 = new int;

p2 = new int;

*p1 = 10;

*p2 = 20;

cout << *p1 << " " << *p2 << endl;

*p1 = *p2;

cout << *p1 << " " << *p2 << endl;

*p1 = 30;

cout << *p1 << " " << *p2 << endl;

 

5.      Describe the action of the new operator. What does the new operator return? What are the indications of error?

 

 

 

 

6.      Your program creates a dynamically allocated array as follows:

int *entry;

entry = new int[10];

so that the pointer variable entry is pointing to the dynamically allocated array. Write code to fill this array with 10 numbers typed in at the keyboard.