UMBC CMSC201, Computer Science I, Fall 1994
Sections 0101, 0102 and Honors
Thursday December 1, 1994
Assigned Reading: 13.3 - 13.4
Handouts (available on-line):
Project 5
Topics Covered:
- We quickly reviewed of previous lecture on pointers.
In this program and sample run,
we use dereferenced integer pointers in integers expressions just like
any other integer variable.
- We use pointers in this program to
pass integer variables to a function by reference. The function
stores the number of hours and minutes in the variables whose
addresses are stored in the parameters.
Sample run.
- We examined parameter passing by reference in greater detail
in the next example. Program and sample
run.
- Using parameters passed by reference makes it possible
to write a function that swaps the contents of two variables
Program and sample run.
In the second version of this function, we print out
the addresses of the variables.
Second program and sample run.
In the next version of the program, we deliberately forget
to use a function prototype for the swap_integers
function. Then, if we mistakenly pass the values of
the variables a and b, executing the program
will cause a core dump. If the prototype were in place,
the compiler would have caught the error.
Program and sample run.
- A word of warning: a function should never return the
address of a local variable to the function that called it.
This could cause all sorts of problems as the next
example shows. Program and sample run.
- Another word of warning: you are allowed to add constants to
a pointer, but the result may not be what you think. The following
example shows that adding 1 to a character pointer, an integer pointer
and a double pointer, adds 1, 4 and 8 respectively to the addresses
stored in the pointers.
Program and sample run.
- The reason that pointer addition is defined this way because
we want to make it easy for pointers to point to successive
elements of an array. Here is an example:
program and
sample run.
In fact, if a variable A is a pointer to the
beginning of an array, then the first element of the array
can be accessed by deferencing A using the
expression *A or by the array element notation
A[0]. When a formal parameter is declared to
be an array, it is really a pointer to an array as
shown in the following
program and
sample run.
- However, there is a difference between pointers and arrays.
When an integer pointer is declared, the compiler sets aside
4 bytes of memory to hold an address. When an integer array,
say with 10 elements, is declared the compiler sets aside
40 bytes of memory to hold 10 integers. We can have a pointer
hold the address of an array, but this does not set aside
any memory to hold the array. Also, we cannot assign the
address of an array to another array, this would be
a syntax error: program and
sample run.
- We can set aside (or allocate) memory dynamically
(when the program is executed, not when it is compiled)
by calling the malloc function. This function
returns the address of the array which should be stored
in a pointer variable. The amount of memory allocated
for the array is given as the actual argument when the
malloc function is called. For example, to
allocate memory for array of integers with 10 elements,
we should make the function call malloc(40).
The following example uses malloc
to create an array dynamically:
Program and
sample run.
- If the malloc function is unable to set aside
the requested amount of memory then the return value is NULL
(which is defined to be zero). You should always check to see if
the return value of malloc is NULL before
proceeding.
The NewArray function in the genlib.h interface
does this for you. You will need either NewArray or
malloc for Project 5.
Last Modified:
Fri Dec 2 16:50:42 EST 1994
Richard Chang, chang@gl.umbc.edu