Книга: Advanced PIC Microcontroller Projects in C
4.1.2 Passing Arrays to Functions
Разделы на этой странице:
4.1.2 Passing Arrays to Functions
There are many applications where we may want to pass arrays to functions. Passing a single array element is straightforward, as we simply specify the index of the array element to be passed, as in the following function call which passes the second element (index = 1) of array A to function Calc. It is important to realize that an individual array element is passed by value (i.e., a copy of the array element is passed to the function):
x = Calc(A[1]);
In some applications we may want to pass complete arrays to functions. An array name can be used as an argument to a function, thus permitting the entire array to be passed. To pass a complete array to a function, the array name must appear by itself within the brackets. The size of the array is not specified within the formal argument declaration. In the function header the array name must be specified with a pair of empty brackets. It is important to realize that when a complete array is passed to a function, what is actually passed is not a copy of the array but the address of the first element of the array (i.e., the array elements are passed by reference, which means that the original array elements can be modified inside the function).
Some examples follow that illustrate the passing of a complete array to a function.
Example 4.6
Write a program to store the numbers 1 to 10 in an array called Numbers. Then call a function named Average to calculate the average of these numbers.
Solution 4.6
The required program listing is shown in Figure 4.8. Function Average receives the elements of array Numbers and calculates the average of the array elements.
/********************************************************************
PASSING AN ARRAY TO A FUNCTION
==============================
This program stores numbers 1 to 10 in an array called Numbers. Function
Average is then called to calculate the average of these numbers.
Programmer: Dogan Ibrahim
File: AVERAGE.C
Date: May, 2007
*********************************************************************/
/* Function to calculate the average */
float Average(int A[]) {
float Sum = 0.0, k;
unsigned char j;
for (j=0; j<10; j++) {
Sum = Sum + A[j];
}
k = Sum / 10.0;
return k;
}
/* Start of the main program */
void main() {
unsigned char j;
float Avrg;
int Numbers[10];
for (j=0; j<10; j++) Numbers[j] = j+1;
Avrg = Average(Numbers);
}
Figure 4.8: Program passing an array to a function
Example 4.7
Repeat Example 4.6, but this time define the array size at the beginning of the program and then pass the array size to the function.
Solution 4.7
The required program listing is shown in Figure 4.9.
/*********************************************************************
PASSING AN ARRAY TO A FUNCTION
==============================
This program stores numbers 1 to N in an array called Numbers where N is
defined at the beginning of the program. Function Average is then called to
calculate the average of these numbers.
Programmer: Dogan Ibrahim
File: AVERAGE2.C
Date: May, 2007
***********************************************************************/
#define Array_Size 20
/* Function to calculate the average */
float Average(int A[], int N) {
float Sum = 0.0, k;
unsigned char j;
for (j=0; j<N; j++) {
Sum = Sum + A[j];
}
k = Sum / N;
return k;
}
/* Start of the main program */
void main() {
unsigned char j;
float Avrg;
int Numbers[Array_Size];
for (j=0; j<Array_Size; j++) Numbers[j] = j+1;
Avrg = Average(Numbers, Array_Size);
}
Figure 4.9: Another program passing an array to a function
It is also possible to pass a complete array to a function using pointers. The address of the first element of the array is passed to the function, and the function can then manipulate the array as required using pointer operations. An example follows.
Example 4.8
Repeat Example 4.6, but this time use a pointer to pass the array elements to the function.
Solution 4.8
The required program listing is given in Figure 4.10. An integer pointer is used to pass the array elements to the function, and the function elements are manipulated using pointer operations. Notice that the address of the first element of the array is passed as an integer with the statement: &Numbers[0].
/********************************************************************
PASSING AN ARRAY TO A FUNCTION
==============================
This program stores numbers 1 to 10 in an array called Numbers. Function
Average is then called to calculate the average of these numbers.
Programmer: Dogan Ibrahim
File: AVERAGE3.C
Date: May, 2007
*********************************************************************/
/* Function to calculate the average */
float Average(int *A) {
float Sum = 0.0, k;
unsigned char j;
for (j=0; j<10; j++) {
Sum = Sum + *(A + j);
}
k = Sum / 10.0;
return k;
}
/* Start of the main program */
void main() {
unsigned char j;
float Avrg;
int Numbers[10];
for(j=0; j<10; j++) Numbers[j] = j+1;
Avrg = Average(&Numbers[0]);
}
Figure 4.10: Program passing an array using pointers