UMBC CMSC202, Computer Science II, Spring 1998,
Sections 0101, 0102, 0103, 0104 and Honors
Thursday March 5, 1998
- A Book on C:
- Programming Abstractions in C:
- Understanding Pointers in C: Chapters 4-6
Handouts (available on-line):
- Revisited pointers and types. Recall that a pointer to int
is used to in the array notation to access an element of an array.
For example, ptr2 in the fourth
example from the previous lecture is a pointer to int. Then,
the array notation ptr2[i] is used to access the i-th
element of the array, assuming that ptr2 points to the
first element of that array. Also, note that the notation
*(ptr2 + i) is equivalent to the notation
The next question is: suppose ptr1 is a pointer to an array
(as in the example above), then what does ptr1[i] mean?
The answer is that this is only meaningful if you have an array
of arrays. In the next example,
SuperArray is an array of int_array's.
Each int_array is an array of 17 integers. Now, if we
assign the address of the first element of SuperArray
to ArrayPtr, then ArrayPtr refers to the
third element of SuperArray, which is itself an array.
In fact, we can continue on and use the notation
ArrayPtr to refer to the 14th integer element of the
array that is the third element of SuperArray. So, in fact
SuperArray is exactly a two dimensional array.
(See sample run.)
- Revisited the realloc() function. In the
program from the previous lecture,
we used realloc() to extend the size of an array that was
previously allocated through malloc(). Calling
realloc() may cause the realloc() to copy the entire
array to a new location. To avoid doing to many copies, we double the
size of the array each time we reallocate memory for the array.
This ensures that we do at most n copies for when the user enters
n numbers. If we only increased the size of the array by 1 each time we
reallocate, then we may do as many as 0.5 n2 copies. So, it
is really important that we double the size of the array each time we
reallocate. We need not worry about wasting space with an array that is
too large, because after the user has entered all the numbers, we can
call reallocate again --- this time to reduce the size of the array to
exactly the length needed to store the n numbers that the user entered.
- Discussed Project 3
extensively. One point that was neglected in the handout:
row and column indices will start from 0.
26 Mar 1998 12:30:09 EST
to Spring 1998 CMSC 202 Section Homepage