Puzzles F01 ... F10 — Pointers

These puzzles involve pointers. Most involve figuring out what a code sample writes on the monitor.

These examples are NOT examples of good programming practice. They show how pointers work and give you practice using them, but do not show how pointers are used in typical programs. Later puzzle chapters show that.


Puzzle F01

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  int  a ;
  int *pa ;
  
  a = 77 ;
  pa = &a ;
  
  printf("a=%d   *pa=%d\n", a, *pa );
  
  system("pause");
  return 0;
}

The variable a is declared to be an integer variable. The variable pa is declared to hold a pointer to an integer. Notice that the name of the second variable is pa. The name does not include the asterisk.


Puzzle F02

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  int  a = 77 ;
  int *pa = &a ;
 
  printf("a=%d   *pa=%d\n", a, *pa );
  
  system("pause");
  return 0;
}

In this example, the variable a is declared to be an integer variable and assigned the value 77, all in one statement. The variable pa is declared to hold a pointer to an integer and pointed at a, all in one statement.


Puzzle F03

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  int  a ;
  int *pa ;
  
  pa  = &a ;
  *pa = 77 ;
 
  printf("a=%d   *pa=%d\n", a, *pa );
  
  system("pause");
  return 0;
}

In this example, the variables a and pa have the same types as previously. In the third statement, pa is set to point at a. It doesn't matter that a has not been initialized yet; pa points at the "little box of memory" that has the name a.

The fourth statement *pa = 77 works like this:

Step 1: The expression on the right of the = is evaluated. This results in the value 77.
Step 2: The expression on the left of the = determines where to put the 77. The *pa says to follow the pointer in pa, so the 77 is put in the variable a.


Puzzle F04

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  int  a = 99;
  int *pa ;
  int *qa ;
  
  pa  = &a ;
  
  qa  = pa ;
 
  printf("a=%d *pa=%d *qa=%d\n", a, *pa, *qa );
  
  system("pause");
  return 0;
}

In this example, the variable a is an int, as previously. But now there are two variables, pa and qa that can point to an int.

The fourth statement pa = &a points pa to a.

The fifth statement qa = pa copies whatever value is in pa to qa. This is just like any assignment statement. But in this example the variable pa contains a pointer to a so that is what is copied into qa.

The statement qa = &a would have the same effect, since it also copies a point to to a into qa.


Puzzle F05

Will the following code compile?

#include <stdio.h>
int main( void )
{
  int  a ;
  int *pa = &a ;
  
  &a = 77 ;
 
  printf("a=%d   *pa=%d\n", a, *pa );
  
  system("pause");
  return 0;
}


Puzzle F06

Will the following code compile and run?

#include <stdio.h>
int main( void )
{
  int  a = 45 ;
  int  *pa = &a ;
 
  printf("a=%d  pa=%o\n", a, pa );  /* Inspect this statement */
  
  system("pause");
  return 0;
}


Puzzle F07

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  int  a = 44 ;
  int  b = 66 ;
  int *p ;
  
  p = &a ; 
  printf("*p=%d\n", *p );
  
  p = &b ; 
  printf("*p=%d\n", *p );
  
  system("pause");
  return 0;
}


Puzzle F08

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  int  a = 44 ;
  int  b = 66 ;
  int *pa, *pb ;
  
  pa = &a ; 
  pb = &b ; 
  printf("*pa=%d *pb=%d\n", *pa, *pb );
  
  *pa = *pb ; 
  printf("a=%d b=%d\n", a, b );
  
  system("pause");
  return 0;
}


Puzzle F09 — swapping pointers

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  int  a = 44 ;
  int  b = 66 ;
  int *pa, *pb, *pt ;
  
  pa = &a ; 
  pb = &b ; 
  printf("*pa=%d *pb=%d\n", *pa, *pb );
  
  /* Swap pointer values */
  pt = pa;
  pa = pb;
  pb = pt;
 
  printf("*pa=%d *pb=%d\n", *pa, *pb );
  
  system("pause");
  return 0;
}

The dotted lines in the diagram show the situation at the time of the first printf(). The solid lines show the final situation.


Puzzle F10

What does the following code write to the monitor?

#include <stdio.h>
int main( void )
{
  double   a = 123.456 ;
  double *pa = &a ;
  
  printf("a=%lf *pa=%lf\n", a, *pa );
  
  system("pause");
  return 0;
}


— Return to the main contents page