Test 7 CS 23021 Name___________________
1. (8 points) State the 4 things to keep in mind when working with pointers.
1. A pointer is a variable whose value is the address of some other variable/object or 0.
2. A dereferenced pointer is an alias of the object the pointer is pointing.
3. If a pointer is pointing to an object of type T, then when that pointer is dereferenced it
is an object of type T.
4. Just because we have a pointer to an object of type T doesn't mean it can be
dereferenced and used, the pointer must be made to point to an object of type T, either an
existing object or a dynamically allocated object.
2. (2 points) What is dynamic storage duration? Dynamic storage duration is controlled by the
programmer. Any variables or objects a created by the programmer with the new keyword
and exist until they are destroyed by the programmer with the delete keyword.
3. (4 points) Dynamically create an integer array with 4 elements, accessed by ptrIntArr.
int *ptrIntArr = new int[4];
4. (3 points) How do you pass values to the main function?
Arguments are passed through the int argc and char* argv[] parameters into main from
the command line. These command line arguments are received by main when they are
typed in after the name of the executable.
5. (2 points) What is a C-style string? It’s a character array that has a null character (‘\0’)
marker at the end to indicate the end of the C-style string.
6. (12 points) Add code to do the following and draw a diagram
a. Define a pointer to point to integers named ptr.
b. Initialize ptr to point to the variable called first.
c. Set the value of the variable first to 12 using ptr.
d. Set the value of the variable second to 35.
e. Draw a diagram that depicts the activities above.
int first, second;
int *ptr; // a.
ptr = &first; //b.
first = 12; // OR *ptr = 12; for c.
second = 35; // d.