同其它变量一样,数组也可以作函数的参数。数组元素只能作函数实参,且同其它变量的用法没有区别。
我们已经知道,数组元素要通过数组名和相应的下标一个个地引用,而数组名可以作函数的实参和形参。当数组作为函数参数时,调用函数中的实参数组只是传送该数组在内存中的首地址,即调用函数通知被调函数在内存中的什么地方找到该数组。在前面我们已经知道了函数参数的值传递方式,调用函数向被调函数传递数据地址的方式,称之为函数参数的引用传递。
对于函数参数的引用传递,除传送数组名外,调用函数还必须通知被调函数:数组有多少个元素。所以,有数组参数的 函数原型的一般形式为:
类型说明符 函数名(数组参数, 数组元素个数)
数组参数并不指定数组元素的个数,即[]中是空的,没有数字。例如6-21:
我们已经知道:函数参数的引用传递不同于值传递。值传递时是把实参的值复制到形参,实参和形参占用不同的存储单元,形参值的改变不会影响到实参。而 数组作为函数参数传递时,是引用传递方式,即把实参数组在内存中的首地址传给了形参,被调函数可以通过该地址,找到实参数组中的各个元素。这就意味着:在被调函数中,如果改变了形参数组某元素的值,在被调函数中,实参数组对应元素的值也会发生相应的改变。
例6-21
例6-22
下面我们看看应该怎样调用上面的mean函数:
例6-23
传值调用与传址调用的区别在于,使用传值调用时,函数接收的是函数值的一份拷贝;另一方面,使用传址调用时,函数接收的是变量的内存地址。因此,函数能改变存放于指定内存单元(也就是变量值)的值,所作的改变在函数结束后继续保留下来。
函数参数的引用传递是有用的。例如当数组作函数参数时,仅仅传送数组在内存中的首地址,避免了复制每一个数组元素,可以节省机器的内存和运行时间。另外, 由于函数中return语句只能带回一个返回值,如果被调函数中有多个返回值,我们常常通过数组带回。但对引用传递应该 注意: 被调函数中对形参数椐的不恰当的改变,会破坏调用函数中的实参数据。
用数组作参数,本身就可以改变数组元素的值,不需要再返回数组,况且也不能返回数组,只能是指针。如果array是return在函数中,这在函数执行完后会回收array占用的地址。如果这时return指针的话,只是一个空地址。可以使用static是array成为static local variable,或者new(c++才有new和delete,c中用malloc,释放用free),这样函数完成时不会释放它占用的内存空间。
或者通过struct, 返回struct中的array。
P.S.
The C language does not let you return an array unless you either
(a) just return a pointer to the type of whatever the array is, and you have to allocate that array, eg you return an "int *".
(b) use a typedef to define the array, and then return that..
数组作为函数参数举例:
1. 顺序查找
查找是计算机中经常要遇到的一种操作,其含义是在一组数据中查找到待查数据的位置。当一组数据无序时,一般采用顺序查找。顺序查找是把给定的值与这组数据中的每个值顺序比较,如果找到,就输出这个值的位置,如果找不到则报告没有找到。下面是顺序查找的程序:
例6-24
运行该程序并输入:
Input 10 numbers:
1 2 3 4 5 6 7 8 9 0↙
Input a key you want to search:
6↙
输出结果为:
The index of the key is 5
本程序中使用了一个数组作为参数,函数seq_search中形参v没有指定数组的长度,要处理的元素的个数由其后所跟的参数n决定。
2. 把一个字符串反序后输出
例6-25
运行该程序并输入:
Input a string:abcd↙
运行结果为:
The string is reversed:dbca
这个程序中,函数reverse没有指定数组元素个数的参数,这是因为字符串总是以一个'{content}'结束,通过'{content}'就可以得到字符数组中元素的个数,也就是字符串的长度,这里我们可以看到使用字符串结束标识的好处。
二维数组也可作为函数参数,第一维的大小可以省略,而第二维的大小不能省略。下面是一个二维数组作为函数参数的例子:
例6-26
我们已经知道,数组元素要通过数组名和相应的下标一个个地引用,而数组名可以作函数的实参和形参。当数组作为函数参数时,调用函数中的实参数组只是传送该数组在内存中的首地址,即调用函数通知被调函数在内存中的什么地方找到该数组。在前面我们已经知道了函数参数的值传递方式,调用函数向被调函数传递数据地址的方式,称之为函数参数的引用传递。
对于函数参数的引用传递,除传送数组名外,调用函数还必须通知被调函数:数组有多少个元素。所以,有数组参数的 函数原型的一般形式为:
类型说明符 函数名(数组参数, 数组元素个数)
数组参数并不指定数组元素的个数,即[]中是空的,没有数字。例如6-21:
我们已经知道:函数参数的引用传递不同于值传递。值传递时是把实参的值复制到形参,实参和形参占用不同的存储单元,形参值的改变不会影响到实参。而 数组作为函数参数传递时,是引用传递方式,即把实参数组在内存中的首地址传给了形参,被调函数可以通过该地址,找到实参数组中的各个元素。这就意味着:在被调函数中,如果改变了形参数组某元素的值,在被调函数中,实参数组对应元素的值也会发生相应的改变。
例6-21
- #include <iostream.h>
- int add(int x, int n)
- {
- return (x + n);
- }
- void main()
- {
- int a[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
- int i;
- for( i = 0; i<10; i++)
- a[i]=add(a[i], i); //数组元素作函数参数
- for(i = 0; i<10; i++)
- cout << a[i] << endl;
- }
例6-22
- double mean(double data_array[], int numelements)
- {
- double sum = 0.0;
- for(int i=0; i<numelements; i++)
- sum += data_array[i];
- return sum / numelements;
- }
下面我们看看应该怎样调用上面的mean函数:
例6-23
- int main()
- {
- double heights[100];
- double h;
- int n = 0;
- cout << "Enter heights:" << endl;
- cin >> h;
- while(h > 0.0) {
- heights[n] = h;
- n++;
- cin >> h;
- }
- double m = mean(heights, n);
- cout << "The mean is " << m << endl;
- return 0;
- }
传值调用与传址调用的区别在于,使用传值调用时,函数接收的是函数值的一份拷贝;另一方面,使用传址调用时,函数接收的是变量的内存地址。因此,函数能改变存放于指定内存单元(也就是变量值)的值,所作的改变在函数结束后继续保留下来。
函数参数的引用传递是有用的。例如当数组作函数参数时,仅仅传送数组在内存中的首地址,避免了复制每一个数组元素,可以节省机器的内存和运行时间。另外, 由于函数中return语句只能带回一个返回值,如果被调函数中有多个返回值,我们常常通过数组带回。但对引用传递应该 注意: 被调函数中对形参数椐的不恰当的改变,会破坏调用函数中的实参数据。
用数组作参数,本身就可以改变数组元素的值,不需要再返回数组,况且也不能返回数组,只能是指针。如果array是return在函数中,这在函数执行完后会回收array占用的地址。如果这时return指针的话,只是一个空地址。可以使用static是array成为static local variable,或者new(c++才有new和delete,c中用malloc,释放用free),这样函数完成时不会释放它占用的内存空间。
- #include <iostream>
- using namespace std;
- int* foo()
- {
- int *arr = new int[10]; //or static int arr[10];
- for (int i=0; i<10; i++)
- arr[i] = i;
- return arr;
- }
- int main(){
- int *arr=0;
- arr = foo();
- for (int i=0; i<10; i++)
- cout << arr[i];
- delete [] arr;
- }
或者通过struct, 返回struct中的array。
- struct mystruct {
- int vector[20];
- }
- struct mystruct foo()
- {
- struct mystruct bar;
- ...do something nifty with bar...
- return bar;
- }
P.S.
The C language does not let you return an array unless you either
(a) just return a pointer to the type of whatever the array is, and you have to allocate that array, eg you return an "int *".
(b) use a typedef to define the array, and then return that..
- typedef int array_int3[4];
- array_int3 func(...)
- {
- array_int3 ret={1,2,3,4};
- return ret;
- }
数组作为函数参数举例:
1. 顺序查找
查找是计算机中经常要遇到的一种操作,其含义是在一组数据中查找到待查数据的位置。当一组数据无序时,一般采用顺序查找。顺序查找是把给定的值与这组数据中的每个值顺序比较,如果找到,就输出这个值的位置,如果找不到则报告没有找到。下面是顺序查找的程序:
例6-24
- #include <stdio.h>
- #define SIZE 10
- //求给定值key的位置,找到则返回其下标,找不到返回-1
- int seq_search(int v[],int n,int key) //v[]:数组参数 n:数组元素个数 key:待查的值
- {
- int i;
- for(i = 0;i < n;i ++)
- if(key == v[i])
- return i;
- return -1; //没找到
- }
- void main(void)
- {
- int d[SIZE],KEY,i,index;
- printf("Input %d numbers:/n",SIZE);
- for(i = 0;i < SIZE;i ++)
- scanf("%d",&d[i]);
- printf("Input a key you want to search:/n");
- scanf("%d",&KEY);
- index = seq_search(d,SIZE,KEY);
- if(index >= 0)
- printf("The index of the key is %d./n",index);
- else
- printf("Not found./n");
- }
运行该程序并输入:
Input 10 numbers:
1 2 3 4 5 6 7 8 9 0↙
Input a key you want to search:
6↙
输出结果为:
The index of the key is 5
本程序中使用了一个数组作为参数,函数seq_search中形参v没有指定数组的长度,要处理的元素的个数由其后所跟的参数n决定。
2. 把一个字符串反序后输出
例6-25
- #include <iostream.h>
- #include <string.h>
- #define LENGTH 80
- //反序一个字符串
- void reverse(char s[])
- {
- char c;
- int i,j;
- j = strlen(s) - 1;
- for(i = 0;i < j;i ++)
- {
- c = s[i];
- s[i] = s[j];
- s[j] = c;
- j --;
- }
- }
- void main(void)
- {
- char str[LENGTH + 1];
- cout<<"Input a string:";
- cin>>str;
- reverse(str);
- cout<<"The string is reversed:"<<str<<endl;
- }
运行该程序并输入:
Input a string:abcd↙
运行结果为:
The string is reversed:dbca
这个程序中,函数reverse没有指定数组元素个数的参数,这是因为字符串总是以一个'{content}'结束,通过'{content}'就可以得到字符数组中元素的个数,也就是字符串的长度,这里我们可以看到使用字符串结束标识的好处。
二维数组也可作为函数参数,第一维的大小可以省略,而第二维的大小不能省略。下面是一个二维数组作为函数参数的例子:
例6-26
- #include <stdio.h>
- void main()
- {
- void add(int x[][4], int n, int num);
- void print(int x[][4], int n);
- int a[3][4]={{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
- int number=10;
- add(a, 3, number);
- print(a, 3);
- }
- void add(int x[][4], int n, int num)
- {
- for(int i = 0; i<n; i++)
- for(int j = 0; j<4; j++)
- x[i][j] = x[i][j]+num;
- }
- void print(int x[][4], int n)
- {
- for(int i = 0; i<n; i++)
- for(int j = 0; j<4; j++)
- printf("%4d/n", x[i][j]);
- }