Puzzle P3

pointer pa pointing to int a

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 );

  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.


Previous Page        Answer         Next Page         Home