Cprogramming 简明教程
Passing Arrays as Function Arguments in C
如果您想将数组传递给一个函数,可以使用 call by value 或 call by reference 方法。在按值调用方法中,函数参数应为已初始化的数组,或大小等于要传递的数组大小的固定大小数组。在按引用调用方法中,函数参数是指向数组的指针。
If you want to pass an array to a function, you can use either call by value or call by reference method. In call by value method, the argument to the function should be an initialized array, or an array of fixed size equal to the size of the array to be passed. In call by reference method, the function argument is a pointer to the array.
Pass array with call by value method
在以下代码中, main() function 具有整数的 array 。通过向其传递数组来调用用户定义函数 average ()。average() 函数接收数组,并使用 for loop 添加其元素。它返回一个浮点值,表示数组中数字的平均值。
In the following code, the main() function has an array of integers. A user−defined function average () is called by passing the array to it. The average() function receives the array, and adds its elements using a for loop. It returns a float value representing the average of numbers in the array.
Example
#include <stdio.h>
float average(int arr[5]);
int main(){
int arr[] = {10, 34, 21, 78, 5};
float avg = average(arr);
printf("average: %f", avg);
}
float average(int arr[5]){
int sum=0;
int i;
for (i=0; i<5; i++){
printf("arr[%d]: %d\n", i, arr[i]);
sum+=arr[i];
}
return (float)sum/5;
}
arr[0]: 10
arr[1]: 34
arr[2]: 21
arr[3]: 78
arr[4]: 5
average: 29.600000
在以下变体中,average() 函数被定义有两个参数,即未初始化且未指定任何大小的数组。通过将数组大小除以 int data type 的大小来获得在 main() 函数中声明的数组的长度。
In the following variation, the average() function is defined with two arguments, an uninitialized array without any size specified. The length of the array declared in main() function is obtained by divising the size of the array with the size of int data type.
Example
#include <stdio.h>
float average(int arr[], int length);
int main(){
int arr[] = {10, 34, 21, 78, 5};
int length = sizeof(arr)/sizeof(int);
float avg = average(arr, length);
printf("average: %f", avg);
}
float average(int arr[], int length){
int sum=0;
int i;
for (i=0; i<length; i++){
printf("arr[%d]: %d\n", i, arr[i]);
sum+=arr[i];
}
return (float)sum/length;
}
arr[0]: 10
arr[1]: 34
arr[2]: 21
arr[3]: 78
arr[4]: 5
average: 29.600000
Pass array with call by reference
为了使用此方法,我们应该了解数组中的元素具有相似的 data 类型,存储在连续的 memory 位置中,并且数组的大小取决于 data 类型。此外,第 0 个元素的地址是 pointer to the array 。
To use this approach, we should understand that elements in an array are of similar data type, stored in continuous memory locations, and the array size depends on the data type. Also, the address of the 0th element is the pointer to the array.
在以下示例中 -
In the following example −
int a[5] = {1,2,3,4,5};
数组的大小是 20 个字节(每个 int 为 4 个字节)
The size of the array is 20 bytes (4 bytes for each int)
Int *x = a;
这里 x 是指向数组的指针。它指向第 0 个元素。如果指针增加 1,它指向下一个元素。
Here x is the pointer to the array. It points to the 0th element. If the pointer is incremented by 1, it points to the next element.
Example
#include <stdio.h>
int main() {
int a[] = {1,2,3,4,5};
int *x = a, i;
for (i=0; i<5; i++){
printf("%d\n", *x);
x++;
}
return 0;
}
1
2
3
4
5
我们利用此特性按引用传递数组。在 main() 函数中,我们声明一个数组并将其地址传递给 max() 函数。max() 函数使用指针遍历数组,并将数组中最大的数字返回给 main() 函数。
Let us use this characteristics for passing the array by reference. In the main() function, we declare an array and pass its address to the max() function. The max() function traverses the array using the pointer and returns the largest number in the array, back to main() function.
Example
#include <stdio.h>
int max(int *arr, int length);
int main(){
int arr[] = {10, 34, 21, 78, 5};
int length = sizeof(arr)/sizeof(int);
int maxnum = max(arr, length);
printf("max: %d", maxnum);
}
int max(int *arr, int length){
int max=*arr;
int i;
for (i=0; i<length; i++){
printf("arr[%d]: %d\n", i, (*arr));
if ((*arr)>max)
max = (*arr);
arr++;
}
return max;
}
arr[0]: 10
arr[1]: 34
arr[2]: 21
arr[3]: 78
arr[4]: 5
max: 78
max() 函数从 main() 中以指针 arr 接收数组地址。每次增加时,它指向原始数组中的下一个元素。
The max() function receives the address of the array from main() in the pointer arr. Each time, when it is incremented, it points to the next element in the original array.
max() 函数也可以像以下定义中那样访问数组元素为常规带下标的数组 -
The max() function can also access the array elements as a normal subscripted array as in the following definition −
int max(int *arr, int length){
int max=*arr;
int i;
for (i=0; i<length; i++){
printf("arr[%d]: %d\n", i, arr[i]);
if (arr[i]>max)
max = arr[i];
}
return max;
}
Pass two−dimensional array to function
您也可以将 two-dimensional array 的指针传递给函数。在函数内部,使用 nested for loop 构造遍历二维数组
You can also pass the pointer of a two-dimensional array to a function. Inside the function, the two dimensional array is traversed with a nested for loop construct
Function to compare string lengths
在以下程序中,将两个 strings 传递给 compare() 函数。在 C 中,由于字符串是 char data 类型的数组。我们使用 strlen() function 查找字符串的长度,即其中的字符数。
In the following program, two strings are passed to compare() functions. In C, as string is an array of char data type. We use strlen() function to find the length of string which is the number of characters in it.
Example
#include <stdio.h>
#include <string.h>
int compare( char *, char *);
int main() {
char a[] = "BAT";
char b[] = "BALL";
int ret = compare(a, b);
return 0;
}
int compare (char *x, char *y){
int val;
if (strlen(x)>strlen(y)){
printf("length of string a is greater than or equal to length of string b");
}
else{
printf("length of string a is less than length of string b");
}
}
length of string a is less than length of string b