An array, like any other kind of value in C, can be passed as an argument to a
function. In a better world that is all we would have to say about array
arguments, since you already know about integer arguments and real arguments
and char arguments. Unfortunately, array arguments in C do not behave the way
that beginning C programmers would expect. In the end, when you understand
more of C, array arguments will make perfect sense. For now, however, they
require careful and detailed study.
So that you can understand what I am talking about, take a look at
``arraydemo.c'' in your examples directory (or view it
directly).
Read the program carefully, and notice that there are three function calls (two
to ``modifyInt'' and one to ``modifyArray'') in the main function. Very
carefully try to predict what will be printed out when you run the program.
Then compile and run the program and see if your intuition is correct.
Here's an explanation for what went on.
- In the first call to ``modifyInt'', we are passing the value of an
integer variable as the argument. Recall that C uses
call-by-value, which means that the dummy argument in ``modifyInt''
(n) contains the value of the actual argument (x), but has nothing
more in common. So when the value of ``n'' is changed, there is no
effect on ``x''. (There's nothing new going on here--we've talked
about this before.)
- In the second call to ``modifyInt'', we are passing the value of
an integer array element as the argument. Since an element of an
integer array behaves identically to a simple integer variable, we
would expect this case to behave just like the previous one. And, in
fact, it does.
- In the call to ``modifyArray'', we are passing the entire integer
array as an argument. You might reasonably assume that the entire
array would be copied during the function call, so that any
modifications to the ``n'' made by ``modifyArray'' would not be
visible in the original version ``y''. (After all, this is what
happened in the first two cases.) But in this case, as you can see,
the assignment to ``n[0]'' in ``modifyArray'' results in a change to
``y[0]'' in the main function. This seems like an inconsistency.
What is going on?
If this seems like an inconsistency, it is because it is an
inconsistency! When you pass an entire array as an argument in C,
nothing is copied. Instead, the address in memory of the actual
array argument is passed up to the function. In effect, the dummy
argument becomes another name for the actual argument, and any
modifications made to the dummy argument are actually made in the
original argument.
In our example, when we call ``modifyArray'' we are passing the
address of ``y''. So the assignment to ``n[0]'' in ``modifyArray'' is
actually an assignment to ``y[0]''. This is known as
``call-by-reference,'' which is how all argument passing is done
in Fortran, and how var parameters are passed in Pascal. For
the time being you should treat this as a special case of argument
passing. (It will make a lot more sense, and seem a lot more
intuitive, once we study pointers.)
This difference between array arguments and other kinds of arguments
may be confusing. Can you think of a good reason for passing array
arguments in this way?
Click here for the answer
Hamlet Project
hamlet@cs.utah.edu