Lecture Notes for Introduction to Computer Science II

2 August 2001 - Pointer Declarations and Basic Operations


  1. pointer declarations

    1. the declaration T * t_ptr; declares a variable of type pointer to T

    2. the * applies only to the following variable name

    3. convention dictates that pointer variables have names that indicate they're pointers - _ptr, ptr, or p

    4. always initialize pointer variables - a real value or 0; avoid the dreaded NULL

    5. two pointer types T1* and T2* are equal if T1 and T2 are the same

      1. i = c is ok; ip = cp is illegal

      2. ints are required to be assignable to pointers, however, it's not required that the assignment make sense

  2. basic pointer operations - address of and follow pointer

    1. the address-of operator &

      1. unary prefix operator &var returns the address of var

      2. if var has type T, &var is a value of type pointer to T

      3. can only apply & to variables - &1 is illegal

    2. the follow-pointer operator *; also indirection or dereferencing operator

      1. unary prefix operator *(expr) returns the value at the location pointed to by expr

      2. expr is often a pointer variable, but not always

      3. if expr has type T*, *(expr) is a value of type T

      4. the pointer-follow operator will follow whatever the expression tells it to, even garbage


This page last modified on 1 August 2001.