Array Parameters
The concepts of passing arrays as parameters follows from the
underlying philosophy of arrays in C. In order to understand array
parameters, it helps to first review the mechanisms available for
passing simple types (e.g., ints, doubles)
to functions in C.
Array Parameters
In C, the declaration
double numberArray [5] = {43.7, 23.1, -56.2, 98.6, -40.0};
allocates space for 5 double precision numbers and
initializes those values. The variable numberArray refers
to the address of the first array
element.
From a compiler's perspective, a reference to the
variable numberArr usually is equivalent to the
expression &numberArray[0]. (Documentation lists
three exceptions, as noted in the given link.)
Because numberArray is actually an address, parameter
passage for arrays involves the base address of the array —
without specifying a ampersand &, the base address
of the array is passed to the function.
As an example, consider the following code:
/* illustration of passing an array parameter */
#include <stdio.h>
/* Function that demonstrates an array parameter whose values are printed,
* modified, and printed again
*/
void arrayFunction(double arrayParam[])
{
int i;
printf ("values of array at start of function: ");
for (i = 0; i < 5; i++)
printf ("%8.2lf", arrayParam[i]);
printf ("\n");
arrayParam[1] += 100;
arrayParam[3] += 300;
printf ("values of array at end of function: ");
for (i = 0; i < 5; i++)
printf ("%8.2lf", arrayParam[i]);
printf ("\n");
}
/* Declare and initialize an array, pass to a function and print the result */
int main ()
{
double numberArray [5] = {43.7, 23.1, -56.2, 98.6, -40.0};
arrayFunction(numberArray);
printf ("values of array at end of main: ");
for (int k = 0; k < 5; k++)
printf ("%8.2lf", numberArray[k]);
printf ("\n");
return 0;
}
When this program is run, initial values are stored in
the numberArray array. When
function arrayFunction is called, the base address of
the numberArray array is copied to
the arrayParam parameter, but the values within the are
are not copied. Thus, array references within
the arrayFunction function refer to the original array —
there is not another copy of the array.
The resulting output from this program follows:
values of array at start of function: 43.70 23.10 -56.20 98.60 -40.00 values of array at end of function: 43.70 123.10 -56.20 398.60 -40.00 values of array at end of main: 43.70 123.10 -56.20 398.60 -40.00
Note that the changes to array elements 2 and 4, made within the function, are recorded in the main array.
Observation
As a secondary observation, note that an array variable
(e.g., numberArray) contains information about where the
array begins. However, the array does NOT contain information about
how long it is or where it stops. Thus, in the sample program, the
programmer had to remember that numberArray was declared
with 5 elements, and this information was hard coded into the program
in both arrayFunction and main.
If a function will be called with several arrays, it is common for an extra parameter (the array length) to be added, so the function will know how many array elements might be involved in processing.
