next up previous
Next: Classes Up: Pointers Previous: Examples

In class exercises

Indicate if the following code fragments are correct and, if so, what value will be printed by the output statement.



int x;
int *y;
x = 4;
y = &x;
*y = 5;
cout << x << endl;

int *x;
int *y;
int ctr;
x = new int [20];
for (ctr=0; ctr<20; ctr++) {
  x[ctr] = ctr;
}
y = x + 1;
cout << y[0] << endl;
delete [] x;

char *x;
char *y;
x = new char[20];
strcpy(x, "Hello world");
y = new char[20];
y = x;
cout << y << endl;
delete x;
delete y;

char *x;
x=new char[2];
strcpy(x, "Hello world");
delete [] x;



Eric Breimer
9/13/1999