Created 3/2/2009, during a winter storm that cancelled classes.

Puzzles F21 ... F30

Pointers to Pointers

A pointer can be declared to point to any type of entity that can exist in memory: ordinary variables, structs (see part G), and pointer variables.

The puzzles here are not examples of practical programming, but are intended as practice of the concept of "pointers to pointers." This concept does have practical (and vital) uses, but you will not see them until later.


Puzzle F21 — Pointer to a Pointer

What does the following code write to the monitor?

#include  <stdio.h> 

void main ( void )
{
  int value;
  int *pv;
  int **ppv;

  value = 32;
  pv = &value;
  ppv = &pv;

  printf("value = %d\n", value );
  printf("*pv   = %d\n", *pv );
  printf("*( *ppv ) = %d\n", *( *ppv ) );
  
  system("pause");
}


Puzzle F22 — The Usual Syntax

What does the following code write to the monitor? This is the same as the previous puzzle, but uses more common syntax in the last printf.

#include  <stdio.h> 

void main ( void )
{
  int value;
  int *pv;
  int **ppv;

  value = 32;
  pv = &value;
  ppv = &pv;

  printf("value = %d\n", value );
  printf("*pv   = %d\n", *pv );
  printf("**ppv = %d\n", **ppv  );  /* slight sytax change */
  
  system("pause");
}


Puzzle F23 — Three Levels of Indirection

As you might fear, you can have pointers to pointers to pointers. Here is an example of that:

#include  <stdio.h> 

void main ( void )
{
  int value;
  int *pv;
  int **ppv;
  int ***pppv;

  value = 32;
  pv = &value;
  ppv = &pv;
  pppv = &ppv;
  
  printf("value = %d\n", value );
  printf("*pv   = %d\n", *pv );
  printf("*(*ppv) = %d\n", *(*ppv) );
  printf("*(*(*pppv)) = %d\n", *(*(*pppv)) );

  system("pause");
}

There is rarely a reason to have more than three levels of indirection (pointer to pointer to pointer to something), although C allows it.

What does this program write to the monitor?


Puzzle F24 — Two Pointers to a Pointer

There is no problem having two pointers point to a single pointer. Here is some code:

#include  <stdio.h> 

void main ( void )
{
  int value;
  int *pv;
  int **ppv;
  int **ppw;

  value = 32;
  ________________________ ;
  ________________________ ;
  ________________________ ; 

  printf("value = %d\n", value );
  printf("**ppv = %d\n", **ppv );
  printf("**ppw = %d\n", **ppw );

  system("pause");
}

Edit the program so that it prints out:

value = 32
**ppv = 32
**ppw = 32
See the picture.


Puzzle F25 — Review

Pointers can point to a place to store a value (the place is called an L-value). Here is a program with just one level of indirection:

#include  <stdio.h> 

void main ( void )
{
  int value;
  int *pv;

  pv = &value;
  
  *pv = 47;  /* *pv on the left designates a location */
  
  printf("value = %d\n", value );
  printf("*pv   = %d\n", *pv );

  system("pause");
}

What does the program write?


Puzzle F26 — Follow two links

Pointers can point to a place which points to a place in which store a value. Here is a program where two links are followed to get to the storage location.

#include  <stdio.h> 

void main ( void )
{
  int value;
  int *pv;
  int **ppv;

  pv  = &value;
  ppv = &pv;

  **ppv = 47;  /* **ppv on the left designates a location */

  printf("value = %d\n", value );
  printf("**ppv   = %d\n", **ppv );

  system("pause");
}

What does the program write?


Puzzle F27 — Stars on both sides

Same ideas as above, but now the ugly statement may take some thought:

#include  <stdio.h> 


void main ( void )
{
  int value, num = 99 ;
  int *pv= &value, *pn = &num ;
  int **ppv= &pv ;
 
  **ppv = *pn;  /* ugly statement */

  printf("**ppv   = %d\n", **ppv );

  system("pause");
}

What does the program write?


Puzzle F28 — Changing a Pointer to a Pointer

Of course, a pointer to a pointer can be changed to point to a new thing:

#include  <stdio.h> 

void main ( void )
{
  int value = 77, num = 99 ;
  int *pv= &value, *pn = &num ;
  int **ppi ;

  ppi = &pv;
  printf("**ppi = %d\n", **ppi );

  ppi = &pn;
  printf("**ppi = %d\n", **ppi );

  system("pause");
}


What does the program write?


Puzzle F29 — Pointing to a Changing Pointer

Of course, the pointer that a pointer to a pointer points to can be changed to point to a new thing:

#include  <stdio.h> 

void main ( void )
{
  int value = 77, num = 99 ;
  int *pv ;
  int **ppi ;

  ppi = &pv;
  
  pv  = &value;
  printf("**ppi = %d\n", **ppi );

  pv  = &num;
  printf("**ppi = %d\n", **ppi );

  system("pause");
}


What does the program write?


Puzzle F30 — difficult

What do you suppose will happen when the following is compiled and run?

#include  <stdio.h> 

void main ( void )
{
  int value;
  int *pv;
  int **ppv;

  value = 32;
  pv = (int *)&ppv;
  ppv = &pv;
  
  printf("value = %d\n", value );
  printf("*pv   = %d\n", *pv );
  printf("**ppv = %d\n", **ppv );

  system("pause");
}

This program will compile and run, but is not a sensible thing to do. It might have come about when a programmer, desperate to get a program to compile in spite of an error, put in a type cast which seemed to fix the problem.


— Return to the main contents page