Puzzle P2

pointer pa pointing to int a

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

  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 is initialized to point at a, all in one statement.

Confusion Alert: Initialization and assignment are not the same!

The declaration

int *pa;

says that the variable pa will point to an int. The asterisk * shows how the variable will be used. The declaration and initialization

int *pa = &a;

declares the variable (as above) and puts the address of a into it.

Later on in the code, after a pointer variable has been declared, put the address of a variable into it like this:

pa = &a;

Carefully note the difference between initialization and assignment.



Answer         Next Page         Previous Page Home