UMBC CS 201, Fall 05
 UMBC CMSC 201 Fall '05 CSEE | 201 | 201 F'05 | lectures | news | help Search Notes:

Even More Good News About Arrays as Parameters

What gets passed to the function?

The address of the base of the array.

So what?

There is a BIG difference between passing values, which we have been doing up until now, and passing addresses.
• When we pass values, a copy is made and placed into a local variable of the function. Changes that are made to that variable in that function do not affect the original value back in the calling function. This is known as call by value .
• When we pass an array to a function, we are really passing an address. We are not making a copy of the array. Changes that are made to the array by the called function change the original array, because we are accessing its elements by using its address. Passing an address is known as call by reference.

Ben and his programmers didn't use the array that way.
But consider this sample program that exploits this behavior.

The Program

/*************************************************\ * Filename: passing.c * * Author: Sue Bogar * * Date Written: Spring 97 * * Section: 101 * * EMail: bogar@cs.umbc.edu * * Modified: 2/25/98 by the author. * * * * This program was designed to show an example * * of passing an array to a function * \*************************************************/ #include <stdio.h> #define SIZE 10 /* Example prototype of a function that takes an array as an argument. NOTE: the empty square brackets. You can optionally put the size within the brackets. */ void Cubes(int cubes[], int numElems); int main ( ) { int array[SIZE], i ; /* Assign each element the value of the index squared */ for (i = 0; i < SIZE; i++) { array[i] = i * i ; } /* Print the values of the elements */ for (i = 0; i < SIZE; i++) { printf("array[%d] = %d\n", i, array[i]) ; } printf("\n") ; /* Example of a function call. NOTE: only the name of the array is passed (the address) */ Cubes(array, SIZE) ; /* Print the values of the elements */ for (i = 0; i < SIZE; i++) { printf("array[%d] = %d\n", i, array[i]) ; } printf("\n") ; return 0; } /******************************************** * Function : Cubes * * Cubes replaces each of the elements in the * array passed in with the cube of the index. * * Input: an array of ints (cubes) * the size of the array (numElems) * Output: there is no return value. ********************************************/ void Cubes(int cubes[], int numElems) { int i ; /* Assign each element the value of the index cubed */ for (i = 0; i < numElems; i++) { cubes[i] = i * i * i ; } }

The Sample Run

array[0] = 0 array[1] = 1 array[2] = 4 array[3] = 9 array[4] = 16 array[5] = 25 array[6] = 36 array[7] = 49 array[8] = 64 array[9] = 81 array[0] = 0 array[1] = 1 array[2] = 8 array[3] = 27 array[4] = 64 array[5] = 125 array[6] = 216 array[7] = 343 array[8] = 512 array[9] = 729

Another Example

/*********************************** ** File: addingarrays.c ** Author: D. Frey ** Date: 2/9/00 ** Section: 303 ** EMail: frey@cs.umbc.edu ** Modified by: Sue Evans ** Mod. Date: 2/21/04 ** ** This program is an example of ** passing arrays to functions. ** ** An array of even integers and an array ** of odd integers are displayed and the ** sum of the corresponding elements is ** calculated and displayed ** ***********************************/ #include <stdio.h> /* number of elements in each array */ #define NUM_ELEMENTS 5 /* function prototypes */ void PrintArray (int array[], int size); void AddArrays (int array1[], int array2[], int totals[], int size); int main ( ) { /* initializing arrays */ int evens [NUM_ELEMENTS] = {2, 8, 12, 14, -4}; int odds [NUM_ELEMENTS] = {3, 5, 7, 9, 17}; int total [NUM_ELEMENTS] = {0}; /* a few blank lines if you please */ printf ("\n\n"); /* display the even integer array */ printf ("Evens:"); printf ("\t"); PrintArray (evens, NUM_ELEMENTS); /* display the odd integer array */ printf ("Odds:"); printf ("\t"); PrintArray (odds, NUM_ELEMENTS); /* add the corresponding elements of the arrays */ AddArrays (evens, odds, total, NUM_ELEMENTS); /* display the totals array */ printf ("Totals:"); printf ("\t"); PrintArray (total, NUM_ELEMENTS); printf ("\n\n"); return 0; } /*********************************** ** Function: PrintArray() ** ** PrintArray() displays the elements of an ** integer array on one line ** ** Input: an integer array and ** the number of elements in the array ** Output: there is no return value ***********************************/ void PrintArray (int array[], int size) { int i; for (i = 0; i < size; i++) { printf ("%4d", array[i]); } printf ("\n"); } /*********************************** ** Function: AddArrays ** ** AddArrays() adds the elements with the ** same index of array1 & array2 and stores ** their sum in the array, totals, at that ** index. Although nothing is returned, the ** array, totals, passed in is modified by ** the function to hold the sums described ** above. ** ** Inputs: 2 integer arrays to be added, ** an integer array to hold the result ** & the size of all arrays ** Output: there is no return value ** Assumptions: all arrays are the same size ***********************************/ void AddArrays (int array1[], int array2[], int totals[], int size) { int i; for (i = 0; i < size; i++) { totals[i] = array1[i] + array2[i]; } }

Sample Output

linux1[108] % a.out Evens: 2 8 12 14 -4 Odds: 3 5 7 9 17 Totals: 5 13 19 23 13