Friday, December 25, 2009

7.3 PASSING ARRAYS TO A FUNCTION

An entire array can be passed to a function as an argument.The manner in which the array is passed differs markedly, however, from that of an ordinary variable.

To pass an array to a function, the array name must appear by itself, without brackets or subscripts, as an actual argument within the function call.The corresponding formal argument is written in the same manner,though it must be declared as an array within the formal argument declarations.when declaring a one-dimensional array as a formal argument,the array name is written with a pair of empty square brackets.The size of the array is not specified within formal argument declaration.

Some care is required when writing function prototypes that include array arguments.An empty pair of square brackets must follow the name of each array argument, thus indicating that the argument is an array.If argument names are not included in a function declaration, then an empty pair of square brackets must follow the array argument data type.

When an array is passed to a function, however, the values of the array elements are not passed to the function.Rather, the array name is interpreted as the address of the first array element (i.e. the address of the memory location containing the first array element).This address is assigned to the corresponding formal argument when the function is called.Therefore the formal argument becomes a pointer to the first array element.Arguments that are passed in this manner are said to be passed by reference rather than by value.

Here is a simple C program that passes a three-element integer array to function, where the array elements are altered.The values of the array elements are displayed at three different places in the program, thus illustrating the effects of alterations.

 #include < stdio.h >


void modify (int a[]);    /*function prototype*/


main()
{
int count,a[3];     /*array definition*/


printf("\nFrom main, before calling the function: \n");
for (count=0; count <= 2; ++count)
{
a[count] = count + 1;
printf("a[%d] =\n", count, a[count]);



modify(a);


printf("\nFrom main, after calling the function: \n");
for (count=0; count <= 2; ++count)
printf("a[%d] =\n", count, a[count]);


}


void modify (int a[])   /*Function definition*/

{
 int count;


printf("nFrom the function, after modifying the values: \n");
for (count=0; count <= 2; ++count) {
a[count] = -9;
printf("a[%d] =\n", count, a[count]);
}
return;

}

The array elements are assigned the values a[0] = 1, a[1] =2 and a[2] =3 within the first loop appearing in  main.These values are displayed as soon as they are assigned.The array is then passed to the function modify, when each array element is assigned the value -9.These new values are then displayed from within the function.Finally the values of the array elements are again displayed from main, after control has been transferred back to main from modify.

When the program is executed, the following output is generated.

From main, before calling the function:
a[0] = 1
a[1] =2 
a[2] =3

From the function, after modifying the values:
a[0] =-9
a[1] =-9
a[2] =-9

From main, after calling the function:

a[0] =-9
a[1] =-9
a[2] =-9

These results show that the elements of a are altered within main as a result of the changes that were made within modify. 

No comments:

Post a Comment