The following exercises should be done by hand - on paper, a tablet, or the whiteboard. There is no assessment for this lab, but it is still very valuable practice - both to understand pointers and to prepare for the midterm.
Declaring Pointer Variables
Draw a memory diagram showing the following variables, leaving space for the pointer variables in the next step:
int i = 10;
int j;
char c;
double avg = 10.3;
A memory diagram is a visual representation of the memory allocated by the program. It should show boxes for each variable labeled with the variable name and the value in the box. For pointers, the value of the pointer should be depicted as an arrow pointing to a different box.
Declare and (if requested) initialize the following pointer variables. Update your memory diagram to show the pointer variables and their values:
- An (uninitialized) pointer to
intnamediptr1 - A pointer to
intnamediptr2, initialized to point atj - Three pointers to
char, one namedcp1pointing atc, one namedcp2left uninitialized, and one namedcp3pointing at “nothing” - A pointer named
ptrthat is pointing atavg
Deducing Pointer Expression Types
Given the declarations from part 1, write the type of each of the following expressions, or state why it produces an error:
| # | Expression | Type |
|---|---|---|
| 1 | i | |
| 2 | &c | |
| 3 | *avg | |
| 4 | ptr | |
| 5 | *cp1 | |
| 6 | (*iptr1)++ | |
| 7 | *(&j) | |
| 8 | &(*cp1) | |
| 9 | &(*avg) |
Reading Pointer Expressions
Each of the following statements will either compile and execute (perhaps in an undefined way), or will fail because of compilation or run-time error. For each, either:
- Update your memory diagram from part 1 or (in the case of
coutstatements) show the output, or - Explain the resulting error, marking a
Cfor compilation error orRfor run-time error in the “Error” column
| Statement | Error? | Explanation of error |
|---|---|---|
c = 'a'; | ||
iptr1 = &i; | ||
*iptr2 = 42; | ||
char *newPtr1 = c; | ||
int *newPtr2 = iptr1; | ||
double *newPtr3 = *iptr2; | ||
double *newPtr4 = ptr; | ||
*iptr1 = *iptr2 + 3; | ||
*iptr2 = iptr1; | ||
iptr2 = iptr1; | ||
*c = 'b'; | ||
ptr* = 5.8; | ||
avg* = 5.8; | ||
iptr2 = *iptr1 - 8; | ||
--(*iptr2); | ||
iptr1 = 0; | ||
cout << *cp1; | ||
cout << *cp2; | ||
cout << cp3; | ||
cout << *cp3; | ||
avg = *iptr2; |
Writing Pointer Expressions
For this question you can use the computer to actually test your code.
Given the declarations:
char c;
double d;
int i;
int j;
char *pc;
double *pd;
int *pi1;
int *pi2 = NULL;
Assume that:
pcis currently pointing atcpdis currently pointing atdpi2may have been changed to point at eitheriorj- all of the non-pointer variables may have been set before the following code segment
Write a segment of C++ code which implements the following algorithm:
if c equals 'x'
then
set d = 12.34
set pi1 to point to j // line 4 β see instructions below
otherwise
set pi1 to point to the same variable as pi2
The catch? You may only use pointer variables, except to refer to j on line 4.
Code walkthrough
For each of the code snippets below, draw memory diagrams and trace the code, showing all output.
double x, y, *p, *q; p = &x; *p = 14.5; q = &y; *q = 23.8; cout << *p << "," << *q << endl; q = p; *p = 36.1; cout << *p << "," << *q << endl; p = NULL; cout << x << "," << y << endl;double x, y, *p, *q; p = &x; *p = 14.5; q = p; if (p == q) cout << "1. yes" << endl; q = &y; if (p == q) cout << "2. yes" << endl; *q = 14.5; if (p == q) cout << "3. yes" << endl; if (*p == *q) cout << "4. yes" << endl;