CS 455 Lecture Notes

Week Six, Tuesday: Pointers and Strings

Chapter 5 of DD completes our review of introductory material.

Pointer Variable Declarations and Initialization

Pointer Operators

Star and Ampersand (* and &) are the dereferencing (AKA indirection) and address-of operators, respectively.
  int y = 5;
  int *yPtr;

  yPtr = &y;

  cout << *yPtr << endl;

  *yPtr = 9;

Calling Functions by Reference

Passing a variable (or object) by pointer allows the called function to operate on that variable (or object). This is not to be confused by "reference parameters" which do much the same thing.

Pointer Arithmetic

One can advance through an array by incrementing the array pointer. Avoid this unless you have a really good reason. Maintainability and portability problems can arise.

Arrays of Pointers

For example a C-string array:
  char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};

Function Pointers

A pointer to a function may be passed as a parameter to another function. This allows function behavior to be modified on the fly to make your code even harder to understand and maintain. Use this technique sparingly (only when you have a good reason to).

Character and String Fundamentals

String library: copy, catenation, comparison.


This page established September 26, 1999; last updated September 27, 1999.