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
4.1.3 Passing Variables by Reference to Functions
By default, arguments to functions are passed by value. Although this method has many distinct advantages, there are occasions when it is more appropriate and also more efficient to pass the address of the arguments instead, that is, to pass the argument by reference. When the address of an argument is passed, the original value of that argument can be modified by the function; thus the function does not have to return any variables. An example follows which illustrates how the address of arguments can be passed to a function and how the values of these arguments can be modified inside the function.
Example 4.9
Write a function named Swap to accept two integer arguments and then to swap the values of these arguments. Use this function in a main program to swap the values of two variables.
Solution 4.9
The required program listing is shown in Figure 4.11. Function Swap is defined as void since it does not return any value. It has two arguments, a and b, and in the function header two integer pointers are used to pass the addresses of these variables. Inside the function body, the value of an argument is accessed by inserting the “*” character in front of the argument. Inside the main program, the addresses of the variables are passed to the function using the “&” character in front of the variable names. At the end of the program, variables p and q are set to 20 and 10 respectively.
/*************************************************************
PASSING VARIABLES BY REFERENCE
==============================
This program shows how the address of variables can be passed to functions.
The function in this program swaps the values of two integer variables.
Programmer: Dogan Ibrahim
File: SWAP.C
Date: May, 2007
***************************************************************/
/* Function to swap two integers */
void Swap(int *a, int *b) {
int temp;
temp = *a; // Store a in temp
*a = *b; // Copy b to a
*b = temp; // Copy temp to b
}
/* Start of the main program */
void main() {
int p, q;
p = 10; // Set p = 10
q = 20; // Set q = 20
swap(p, q); // Swap p and q (p=20, q=10)
}
Figure 4.11: Passing variables by reference to a function
4.1.4 Variable Number of Arguments
The ellipsis character (“...”) consists of three successive periods with no spaces between them. An ellipsis can be used in the argument lists of function prototypes to indicate a variable number of arguments or arguments with varying types. An example of a declaration of a function prototype with ellipsis follows. In this declaration, when the function is called we must supply at least two integer type arguments, and we can also supply any number of additional arguments:
unsigned char MyFunc(int a, int b, ...);
The header file stdarg.h must be included at the beginning of a program that uses a variable number of arguments. This header file defines a new data type called va_list , which is essentially a character pointer. In addition, macro va_start() initializes an object of type va_list to point to the address of the first additional argument presented to the function. To extract the arguments, successive calls to the macro va_arg() must be made, with the character pointer and the type of the parameter as the arguments of va_arg() .
An example program is given in Figure 4.12. In this program the function header declares only one parameter of type int , and an ellipsis is used to declare a variable number of parameters. Variable num is the argument count passed by the calling program. The arguments are read by using the macro va_arg(ap, int) and then summed using variable temp and returned by the function.
/*********************************************************************
PASSING VARIABLE NUMBER OF ARGUMENTS
======================================
This program shows how variable number of arguments can be passed to a
function. The function header declares one integer variable and an ellipsis is
used to declare variable number of parameters. The function adds all the
arguments and returns the sum as an integer. The number of arguments is
supplied by the calling program as the first argument to the function.
Programmer: Dogan Ibrahim
File: VARIABLE.C
Date: May, 2007
***********************************************************************/
#include
/* Function with variable number of parameters */
int Sum(int num, ...) {
unsigned char j;
va_list ap;
int temp = 0;
va_start(ap, num);
for (j = 0; j < num; j++) {
temp = temp + va_arg(ap, int);
}
va_end(ap);
return temp;
}
/* Start of the main program */
void main() {
int p;
p = Sum(2, 3, 5); // 2 arguments. p=3+5=8
p = Sum(3, 2, 5, 6); // 3 arguments, p=2+5+6=13
}
Читать дальше