February 1, 2002

Reading assignment:
  Deitel and Deitel Chapter 5.5 - 5.9
  -or-
  Wang 1.13, 3.9, 4.2

Pointers and arrays are interchangeable
  - Arrays are really pointers to blocks of data
  - If I define
       char arr[20];
    then arr is secretly (and confusingly), actually a POINTER!  It
    is a pointer to the first char in the block of 20.  Since it's not
    an lvalue, you can't change where arr points.  So it is a const
    pointer to a char.  Thus, when you pass an array to a function,
    you're really passing a pointer.  That's why it's automatically
    call-by-reference.
  - Arrays have to be constant pointers, because if you assigned to one,
    the address of the memory location where the array was originally stored
    would be permanently lost and that memory could never be accessed again.
    This "memory leak" would drain away the memory available to the program.
    When we start dynamically memory allocation, we'll have to worry about this
    problem.
  - An important consequence of this is that if a function is
    declared to take a pointer parameter, you can pass it an array.
    arrays_are_pointers.cpp
  - The fact that arrays are really pointers give us another way
    to access arrays.  We'll see that when we discuss pointer arithmetic
  - Just as arrays are really pointers, a pointer can be treated as an array.
  - This gives us 4 ways to access the elements of an array.
    int b[3];
    int *bptr = b;
    b[0] is the same as *(b)   is the same as bptr[0] is the same as *bptr
    b[1] is the same as *(b+1) is the same as bptr[1] is the same as *(bptr+1)
    b[2] is the same is *(b+2) is the same is bptr[2] is the same as *(bptr+2)
    See "Pointer arithmetic" in the next lecture for more on this
  - There's a good program demonstrating this in Deitel and Deitel
    See Figure 5.21

Another example of passing pointers to functions
  - We often have to swap two stored values, as in bubble_sort.  We can accomplish
    this with a function that uses pointers.  Here's a modification of bubble sort
    that does this: bubble_pointerswap.cpp