CS 23021 Pointer Concepts and Usage, Exams of Computer Science

Various concepts related to pointers in c++ including their definition, initialization, dereferencing, pointer arithmetic, and dynamic memory allocation. It also includes examples and diagrams to illustrate these concepts.

Typology: Exams

Pre 2010

Uploaded on 02/25/2010

koofers-user-82g
koofers-user-82g 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
first
12
second
35
ptr
pf2

Partial preview of the text

Download CS 23021 Pointer Concepts and Usage and more Exams Computer Science in PDF only on Docsity!

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. first 12 second 35 ptr
  1. (4 points) Describe pointer arithmetic. Give an example. Pointer arithmetic is a fast and logical way to access data values in an array. Operators include ‘+’, ‘-’, ‘++’, ‘--’ and calculate the base_address + index * sizeof(type). One example would be *(ptr+2). It would access two elements down from ptr’s current address (where ptr is currently pointing to). With the deferencing operator, that element’s value can be accessed and changed.
  2. (12 points) Assume 4 byte ints, 4 byte pointers, and that variables are assigned memory locations in the order that they are defined starting at the address 700. What is output? Include a drawing of the memory and variables. int arr[ 3 ] = { 6 , 24 , 7 }; int *ptr = &arr[ 0 ]; cout << " A " << arr; cout << " B " << ptr; cout << " C " << &arr[ 0 ]; cout << " D " << arr[ 1 ]; cout << " E " << &arr[ 1 ]; cout << " F " << ptr + 1 ; cout << " G " << *(ptr + 1); cout << " H " << *ptr + 1 ; cout << " J " << *ptr; cout << " K " << &ptr; OUTPUT: _ A 700 B 700 C 700 D 24 E 704 F 704 G 24 H 7 J 6 K 712
  3. (6 points) Answer the questions to the right struct Duration { int minutes; int seconds; } struct Song { string artist; string title; Duration time; } struct MusicDB { string name; vector songs; } int func() { MusicDB music; //Add some songs to music } A. What type of object is music.name? string B. What type of object is music.songs? vector of Song’s C. What type of object is music.songs[0]? A struct called Song D. What type of object is music.songs[0].artist? string E. What type of object is music.songs[0].time? A struct called Duration F. What type of object is music.songs[0].time.minutes? int Var Address arr[0] 700 6 arr[1] 704 24 arr[2] 708 7 ptr 712 700