Think back to when we first discussed what happens when you pass arrays as
arguments to functions. How were arrays different from every other data type
that we had studied to that point?
Click here for the answer
When we introduced arrays, we told you to treat them as an exception to the way
that arguments are passed by value in C. Now that you have a little more
perspective, how would you characterize the way that arrays are passed as
arguments in C?
Click here for the answer
Now let's look at the first of a sequence of programs. ``aptr1.c'' in
your ``examples'' directory (or view it directly) passes an integer variable by reference (through
the use of pointers) to a function which initializes the variable.
There's nothing new about this program, but be sure that you
understand it.
Now take a look at ``aptr2.c'' (or view it directly). What's the difference between this
program and the previous one?
Click here for the answer
In the first program we passed a pointer to an integer through which ``init''
communicated the value 17, and in the second program we passed an array of one
element through which ``init'' communicated the value 17.
Now, examine the code of the two programs very carefully and detail exactly
what the differences are.
Click here for the answer
Thus there are four differences between how ``n'' and ``x'' are
declared and used. In ``aptr1.c'' they are declared and used as
pointers (x actually points to ``y''), and in ``aptr2.c'' they are
declared and used as arrays of one element. Yet both programs do more
or less the same thing.
Now try out the following exercise. Change the header of ``init'' in
``aptr1.c'' so that instead of ``int *n'' it contains ``int n[]''.
Compile and run the program. Does it work? Keep making modifications
to both programs in this way, replacing a piece of one program with
the corresponding piece of the other program. Are you surprised at
what happens?
Click here for an observation
Let this sink in before moving along. You may not believe it yet, but the
evidence is all there.
Hamlet Project
hamlet@cs.utah.edu