c中将数组传递给子函数
Whenever we need to pass a list of elements as argument to any function in C language, it is prefered to do so using an array
. But how can we pass an array as argument to a function? Let’s see how its done.
每当我们需要将元素列表作为参数传递给C语言中的任何函数时,最好使用array
传递。 但是如何将数组作为参数传递给函数呢? 让我们看看它是如何完成的。
使用数组作为参数声明函数 (Declaring Function with array as a parameter)
There are two possible ways to do so, one by using call by value and other by using call by reference.
有两种可能的方法,一种是通过按值调用,另一种是通过引用调用。
We can either have an array as a parameter.
我们可以将数组作为参数。
int sum (int arr[]);
Or, we can have a pointer in the parameter list, to hold the base address of our array.
或者,我们可以在参数列表中有一个指针,以保存数组的基地址。
int sum (int* ptr);
We will study the second way in details later when we will study pointers.
稍后我们将研究指针时,我们将详细研究第二种方式。
从函数返回数组 (Returning an Array from a function)
We don’t return an array from functions, rather we return a pointer holding the base address of the array to be returned. But we must, make sure that the array exists after the function ends i.e. the array is not local to the function.
我们不从函数返回数组,而是返回一个指针,该指针保存要返回的数组的基地址。 但是,我们必须确保函数结束后该数组存在,即该数组不在函数本地。
int* sum (int x[])
{
// statements
return x ;
}
We will discuss about this when we will study pointers with arrays.
当我们研究带有数组的指针时,我们将对此进行讨论。
将数组作为参数传递给函数 (Passing arrays as parameter to function)
Now let’s see a few examples where we will pass a single array element as argument to a function, a one dimensional array to a function and a multidimensional array to a function.
现在让我们看几个示例,在该示例中,我们将单个数组元素作为函数的参数传递,将一维数组传递给函数,将多维数组传递给函数。
将单个数组元素传递给函数 (Passing a single array element to a function)
Let’s write a very simple program, where we will declare and define an array of integers in our main()
function and pass one of the array element to a function, which will just print the value of the element.
让我们编写一个非常简单的程序,在其中我们将在main()
函数中声明并定义一个整数数组,并将该数组元素之一传递给一个函数,该函数将仅打印该元素的值。
#include<stdio.h>
void giveMeArray(int a);
int main()
{
int myArray[] = { 2, 3, 4 };
giveMeArray(myArray[2]); //Passing array element myArray[2] only.
return 0;
}
void giveMeArray(int a)
{
printf("%d", a);
}
4
4
将完整的一维数组传递给函数 (Passing a complete One-dimensional array to a function)
To understand how this is done, let’s write a function to find out average of all the elements of the array and print it.
要了解如何做到这一点,让我们编写一个函数来找出数组所有元素的平均值并打印出来。
We will only send in the name of the array as argument, which is nothing but the address of the starting element of the array, or we can say the starting memory address.
我们只将数组的名称作为参数发送,这不过是数组起始元素的地址,或者我们可以说起始内存地址。
#include<stdio.h>
float findAverage(int marks[]);
int main()
{
float avg;
int marks[] = {99, 90, 96, 93, 95};
avg = findAverage(marks); // name of the array is passed as argument.
printf("Average marks = %.1f", avg);
return 0;
}
float findAverage(int marks[])
{
int i, sum = 0;
float avg;
for (i = 0; i <= 4; i++) {
sum += marks[i];
}
avg = (sum / 5);
return avg;
}
94.6
94.6
将多维数组传递给函数 (Passing a Multi-dimensional array to a function)
Here again, we will only pass the name of the array as argument.
再次在这里,我们仅将数组名称作为参数传递。
#include<stdio.h>
void displayArray(int arr[3][3]);
int main()
{
int arr[3][3], i, j;
printf("Please enter 9 numbers for the array: \n");
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
{
scanf("%d", &arr[i][j]);
}
}
// passing the array as argument
displayArray(arr);
return 0;
}
void displayArray(int arr[3][3])
{
int i, j;
printf("The complete array is: \n");
for (i = 0; i < 3; ++i)
{
// getting cursor to new line
printf("\n");
for (j = 0; j < 3; ++j)
{
// \t is used to provide tab space
printf("%d\t", arr[i][j]);
}
}
}
Please enter 9 numbers for the array: 1 2 3 4 5 6 7 8 9 The complete array is: 1 2 3 4 5 6 7 8 9
请为阵列输入9个数字:1 2 3 4 5 6 7 8 9完整的阵列为:1 2 3 4 5 6 7 8 9
翻译自: https://www.studytonight.com/c/array-in-function-in-c.php
c中将数组传递给子函数