Pointers


Operators (in relation to Pointers)

* is the dereferencing operator
and the multiplication operator.

& has no single name. It has multiple uses,
including being the bitwise AND operator
and the address operator.

Each of these operators are interpreted differently
when used in a declaration
than when in code.



Pointer Declaration

int x;     // integer variable
int *px;   // pointer variable



  int x;     // integer variable
  int *px;   // pointer variable

  x = 13;
  px = &x;   // use of address operator


          .       address    label
          .                  or symbol
      |   .    |
      |--------|
      |        |    85         x
      |--------|
      |   .    |
          .     
      |   .    |
      |--------|
      |        |   104         px
      |--------|
      |   .    |
          .     
          .     


  int x;     // integer variable
  int *px;   // pointer variable

  x = 13;
  px = &x;   // use of address operator

  *px = 56;  // dereferencing operator

          .       address    label
          .                  or symbol
      |   .    |
      |--------|
      |        |    85         x
      |--------|
      |   .    |
          .     
      |   .    |
      |--------|
      |        |   104         px
      |--------|
      |   .    |
          .     
          .     


  int x;     // integer variable
  int *px;   // pointer variable

  x = 13;
  px = &x;

  *px = 56;


  x = x - 12;     // 3 statements that would
                  // accomplish the same thing

  *px = x - 12;


  *px = *px - 12;

Allowed Operations on Pointers


  int *px, *py;
  int x, y;

  px = &x;  // get address
  py = &y;

  px = py;  // assignment
            // both lhs and rhs are same type

  px = NULL;  // assignment to NULL

  if ( px == NULL)    // comparison
    printf("pointer is NULL\n");

NOT Allowed on Pointers


 int *px, *py, *pz;

 px = 12;  // assignment to non-NULL literal

 pz = px + py;  // add or subtract with
                // two pointer values

 px = py * 2;   // multiply or divide of
                // pointer values

Draw a Diagram


  int x, y, z;
  int *px, *py, *pz;

  px = &x;
  py = &y;
  pz = &z;

  *px = 24;

  py = px;

  z = 25;

  *py = x - z;

Pointers and Arrays

Efficiency and implementation mean that
arrays and pointers share notation and use.

*(ar + i) is equivalent to ar[i]

and,

&(ar + i) is equivalent to ar + i

  int ar[6]; // array of 6 integers

ar is a label, and represents an address


  *(ar + 2) = -3;  // valid assignment
                   // commonly used syntax

Pointers and Arrays

  int ar[6]; // array of 6 integers
  int *ptr;  // pointer to an integer

ar is a label, and represents an address

  ptr = ar;          // valid assignment

  ar++;              // INVALID

  *(ptr + 1) = 100;  // valid assignment


  // code fragment
  int ar[6]; // array of 6 integers
  int *ptr;  // pointer to an integer

  ptr = ar;
  for (int i = 0; i < 6; i++) {
     *ptr = i * i;
      ptr++;
   }
   for (int j = 0; j < 6; j++) {
      cout << ar[j] << " ";
   }

   // same output, with pointer
   ptr = ar;
   for (int k = 0; k < 6; k++) {
      cout << *(ptr + k) << " ";
   }
   cout << endl;

Example: linked list
(just to show pointer manipulation)

  struct node {
    int value;
    node *next;
  };

  node  *head;
  node *tmp; // for printing the list

  node one =   {1, NULL};
  node two =   {2, NULL};
  node three = {3, NULL};

linked list, continued

  // put one at head of list
  head = &one;

  head->next = &two;

  head->next->next = &three;

  // print list values
  tmp = head;
  while (tmp != NULL) {
    cout << tmp->value << "  ";
    tmp = tmp->next;
  }


linked list, continued

expression type
head pointer to node
head->value int
head->next pointer to node
head->next->value int


Copyright © Karen Miller, 2007, 2008, 2009