Lecture Notes for Introduction to Computer Science II

2 August 2001 - Pointers and Functions


  1. argument passing revisited

    1. parameter changes made inside don't show up outside

    2. formal and actual parameters occupy different parts of memory

    3. changes made in one place don't appear in the other place, and vice versa

  2. passing values through parameters using pointers

    1. isolated changes are not fatal, but it's a pain

    2. to have inside changes appear outside, both places have to be the same, and equal to the outside place

    3. pointers can solve this problem - pass the address of the outside place into the function; side can now change the outside place

    4. passing pointers in parameters is known as call by reference

    5. pointer parameters work, but are clumsy

      1. must remember to proceed actual arguments with the address-of operator &

      2. can't pass expressions because they don't have addresses

      3. formal pointer parameters must be proceeded by the follow-address operator *

  3. passing values through parameters not using pointers

    1. c++ provides a mechanism for true call-by-reference parameters

    2. if T is a type, then T & is the type reference to T

      1. void f(int i) vs. void f(int & i)

    3. a reference parameter has the same address as the actual parameter

      1. it is not necessary to precede the actual parameter with & - expressions are still illegal (sorta)

      2. it is not necessary to precede the formal reference parameter with * - it is a compile-time error;

  4. using the const modifier

    1. pointer and reference parameters are efficient - copying 4 or 8 bytes vs. copying 100s or 1000s of bytes

    2. but they are also unsafe because of propagating changes

    3. the const declaration modifier helps by preventing inside changes

    4. const pointer parameters

      1. two things can be constant - the pointer value and the value pointed to; usually want the value pointed to to be constant

      2. the trick is to read the type declaration from right to left

        1. T * const x - x is a constant pointer to T value

        2. const T * x - x is a pointer to a T value constant

        3. const T * const x - x is a constant pointer to a T value constant

      3. T * const x is not so important - prevents the function from changing the value of x; constants are easier than variables to understand; enables possible compiler optimizations

      4. cde9const T * x) is important - prevents the function from changing the value of what x points to

    5. const reference parameters

      1. because references are already constants, using const with them is easier than with pointers

      2. const reference parameters are fast and safe

      3. const T & x

  5. returning pointer or references values from functions

    1. values local to a function disappear once the function returns

    2. pointers and references to those values are useless after the function returns

    3. int * f(void) {
        int i;
        return &i;
        }
      
        


This page last modified on 2 August 2001.