UMBC CS 201, Spring 02
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.
Mr. Bill 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 *
* SSN: 000-00-0001 *
* 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
#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 b[], int n);
int main ( )
{
int a[SIZE], i ;
/* Assign each element the value of
the index squared */
for (i = 0; i < SIZE; i++)
{
a[i] = i * i ;
}
/* Print the values of the elements */
for (i = 0; i < SIZE; i++)
{
printf("a[%d] = %d\n", i, a[i]) ;
}
printf("\n") ;
/* Example of a function call. NOTE: only the
name of the array is passed (the address) */
Cubes(a, SIZE) ;
/* Print the values of the elements */
for (i = 0; i < SIZE; i++)
{
printf("a[%d] = %d\n", i, a[i]) ;
}
printf("\n") ;
return 0;
}
/********************************************
* Function : Cubes
* Input: an array of ints (b)
* the size of the array (n)
* Output: each element of the array contains
* the value of its index cubed
* there is no return value
********************************************/
void Cubes(int b[], int n)
{
int i ;
/* Assign each element the value of the
index cubed */
for (i = 0; i < n; i++)
{
b[i] = i * i * i ;
}
}
The Sample Run
a[0] = 0
a[1] = 1
a[2] = 4
a[3] = 9
a[4] = 16
a[5] = 25
a[6] = 36
a[7] = 49
a[8] = 64
a[9] = 81
a[0] = 0
a[1] = 1
a[2] = 8
a[3] = 27
a[4] = 64
a[5] = 125
a[6] = 216
a[7] = 343
a[8] = 512
a[9] = 729
Another Example
/***********************************
** File: addingarrays.c
** Author: D. Frey
** Date: 2/9/00
** Section: 303
** SSN: 123-45-6789
** EMail: frey@cs.umbc.edu
**
** 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
/* nr of elements in each array */
#define NRELEMENTS 5
/* function prototypes */
void PrintArray (int a[], int size);
void AddArrays (int a[], int b[], int c[], int size);
int main ( )
{
int evens [NRELEMENTS] = {2, 8, 12, 14, -4};
int odds [NRELEMENTS] = {3, 5, 7, 9, 17};
int total [NRELEMENTS] = {0};
/* a few blank lines if you please */
printf ("\n\n");
/* display the even integer array */
printf ("Evens:");
printf ("\t");
PrintArray (evens, NRELEMENTS);
/* display the odd integer array */
printf ("Odds:");
printf ("\t");
PrintArray (odds, NRELEMENTS);
/* add the corresponding elements of the arrays */
AddArrays (evens, odds, total, NRELEMENTS);
/* display the totals array */
printf ("Totals:");
printf ("\t");
PrintArray (total, NRELEMENTS);
printf ("\n\n");
return 0;
}
/***********************************
** Function: PrintArray
** Input: an integer array and
** the number of elements in the array
** Output: the elements are displayed on one line
** 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
** Inputs: 2 integer arrays to be added
** an integer array to hold the result
** the size of all arrays
** Output: "totals" holds the sum of the
** corresponding element of array1 & array2
** Assumes 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
Last Modified - Thursday, 17-Jan-2002 13:52:10 EST
CSEE
|
201
|
201 S'02
|
lectures
|
news
|
help