HW 4 : Pointer, Dynamic Memory, Structured Data
Exercises
PART A DUE: SUNDAY, April 1st 2007, 11:59PM
PART B DUE: SUNDAY, April 15th 2007, 11:59PM
This Homework is a collection of exercises to help you get started thinking about pointers, dynamic memory, and structured data. Since Spring Break falls in the middle of the Homework we will split the Homework into two parts: Part A (due before Spring Break), Part B (due after Spring Break).
Write your answers in a TEXT file and send it in an e-mail to me: chipp@sci.brooklyn.cuny.edu
Part A (6 Pts)
1. Assume the following definitions:
char c = 'X', d = 'Y', e = 'Z';
char *p1 = &c;
char *p2 = &d;
char *p3;
Assume that the above variables reside at these memory addresses:
Address | Variable |
0x0342 | c |
0x0568 | d |
0x0724 | e |
What is printed to the screen with the follow chunk of code:
p3 = &e;
cout << "*p3 = " << *p3 << endl;
p3 = p1;
cout << "*p3 = " << *p3 << ", p3 = " << p3 << endl;
*p1 = *p2;
cout << "*p1 = " << *p1 << ", p1 = " << p1 << endl;
2. Given these statements:
int * ptr;
int a, b;
a = 10;
b = a;
ptr = &a;
Which of these statements will change the value of the variable a to 20?
A. b = 20
B. *b = 20
C. ptr = 20
D. *ptr = 20
E. Two or more of these statements will change the value of variable a to 20.
3. What is wrong?
int i = 10;
double * dPtr = &i;
4. Give the value of the left-hand side variable in each assignment statement. Assume the lines are executed sequentially. Assume the address of the blocks array is 0x04432.
int main()
{
char blocks[3] = {'A','B','C'};
char *ptr = &blocks[0];
char temp;
temp = blocks[0];
temp = *(blocks + 2);
temp = *(ptr + 1);
temp = *ptr;
ptr = blocks + 1;
temp = *ptr;
temp = *(ptr + 1);
ptr = blocks;
temp = *++ptr;
temp = ++*ptr;
temp = *ptr++;
temp = *ptr;
return 0;
}
5. Write String Reverse Copy. It takes a character string (as a character pointer: char *), allocates a character array the size of the input string, copies the input string in reverse into the dynamically allocated memory, and returns the copy as a character pointer.
USE Character Pointers and NOT array [] notation. Do not use any string or cstring libraries.
The prototype would be:
char * strRevCpy(char * input);
Example of it being used:
char * original = "chipp";
char * myCopy;
myCopy = strRevCpy(original);
cout << myCopy << endl;
delete [] myCopy;
Output would be:
ppihc
Part B (6 Pts)
All of the following questions use the following struct:
struct node {
int id;
node * next;
};
Try figuring out the output by hand first before typing it into a program and running it on the computer.
You might find drawing diagrams of the data and how it points to each other helpful in answering the questions.
6. What is the output? What is the difference between node * one and node two in the way they are declared and initialized?
node * one;
node two;
one = new node;
one->id = 1;
one->next = &two;
two.id = 2;
two.next = one;
cout << one->id << " " << one->next->id << " " << one->next->next->id << endl;
cout << two.id << " " << two.next->id << " " << two.next->next->id << endl;
delete one;
7. What is the output? How are the nodes one, two, and three related to each other?
node * one;
node * two;
node * three;
one = new node;
two = new node;
three = new node;
one->id = 1;
one->next = two;
two->id = 2;
two->next = three;
three->id = 3;
three->next = one;
cout << one->id << one->next->id << one->next->next->id << one->next->next->next->id << endl;
one->next = three;
delete two;
cout << one->id << one->next->id << one->next->next->id << one->next->next->next->id << endl;
delete one;
delete three;
8. What is the output? Describe what the effect the code highlighted in blue below, has upon the three dynamically allocated nodes.
node * start;
node * t;
start = new node;
start->id = 1;
start->next = new node;
start->next->id = 2;
start->next->next = new node;
start->next->next->id = 3;
start->next->next->next = NULL;
for(node * n = start; n != NULL; n = n->next)
{
cout << n->id;
}
cout << endl;
t = start;
start = start->next;
t->next = start->next;
start->next = t;
for(node * n = start; n != NULL; n = n->next)
{
cout << n->id;
}
cout << endl;
for(node * n = start; n != NULL; )
{
t = n;
n = n->next;
delete t;
}
Problems 1 to 5 adapted from these Pointer Exercises.
Last Updated 3/30/2007