Lab 13: Pointers tutorial

Feb 27, 2024  β”‚  m. Feb 27, 2024 by Charlotte Curtis

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:

  1. An (uninitialized) pointer to int named iptr1
  2. A pointer to int named iptr2, initialized to point at j
  3. Three pointers to char, one named cp1 pointing at c, one named cp2 left uninitialized, and one named cp3 pointing at “nothing”
  4. A pointer named ptr that is pointing at avg

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:

#ExpressionType
1i
2&c
3*avg
4ptr
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:

StatementError?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:

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.

  1.  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;
    
  2.  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;
    


Previous: Lab 12: Files and Command Line Arguments, plus separate compilation
Next: Lab 14: Pointers lab