数组作函数的参数和返回值

    同其它变量一样,数组也可以作函数的参数。数组元素只能作函数实参,且同其它变量的用法没有区别。

   我们已经知道,数组元素要通过数组名和相应的下标一个个地引用,而数组名可以作函数的实参和形参。当数组作为函数参数时,调用函数中的实参数组只是传送该数组在内存中的首地址,即调用函数通知被调函数在内存中的什么地方找到该数组。在前面我们已经知道了函数参数的值传递方式,调用函数向被调函数传递数据地址的方式,称之为函数参数的引用传递。

   对于函数参数的引用传递,除传送数组名外,调用函数还必须通知被调函数:数组有多少个元素。所以,有数组参数的 函数原型的一般形式为:
   类型说明符 函数名(数组参数, 数组元素个数)

数组参数并不指定数组元素的个数,即[]中是空的,没有数字。例如6-21:

   我们已经知道:函数参数的引用传递不同于值传递。值传递时是把实参的值复制到形参,实参和形参占用不同的存储单元,形参值的改变不会影响到实参。而 数组作为函数参数传递时,是引用传递方式,即把实参数组在内存中的首地址传给了形参,被调函数可以通过该地址,找到实参数组中的各个元素。这就意味着:在被调函数中,如果改变了形参数组某元素的值,在被调函数中,实参数组对应元素的值也会发生相应的改变。

 
例6-21 
C代码
  1.   #include <iostream.h>    
  2. int add(int x, int n)    
  3. {    
  4.   return (x + n);    
  5. }    
  6. void main()    
  7. {    
  8.   int a[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};    
  9.   int i;    
  10.   for( i = 0; i<10; i++)    
  11.    a[i]=add(a[i], i); //数组元素作函数参数    
  12.   for(i = 0; i<10; i++)    
  13.    cout << a[i] << endl;    
  14. }    



例6-22 
C代码
  1.  double mean(double data_array[], int numelements)    
  2. {    
  3.   double sum = 0.0;    
  4.   for(int i=0; i<numelements; i++)    
  5.    sum += data_array[i];    
  6.   return sum / numelements;    
  7. }   


   下面我们看看应该怎样调用上面的mean函数:    
例6-23 
C代码
  1.   int main()    
  2. {    
  3.   double heights[100];    
  4.   double h;    
  5.   int n = 0;    
  6.   cout << "Enter heights:" << endl;    
  7.   cin >> h;    
  8.   while(h > 0.0) {    
  9.    heights[n] = h;    
  10.    n++;    
  11.    cin >> h;    
  12.   }    
  13.   double m = mean(heights, n);    
  14.   cout << "The mean is " << m << endl;    
  15.   return 0;    
  16. }  
 

  
传值调用与传址调用的区别在于,使用传值调用时,函数接收的是函数值的一份拷贝;另一方面,使用传址调用时,函数接收的是变量的内存地址。因此,函数能改变存放于指定内存单元(也就是变量值)的值,所作的改变在函数结束后继续保留下来。

  
函数参数的引用传递是有用的。例如当数组作函数参数时,仅仅传送数组在内存中的首地址,避免了复制每一个数组元素,可以节省机器的内存和运行时间。另外, 由于函数中return语句只能带回一个返回值,如果被调函数中有多个返回值,我们常常通过数组带回。但对引用传递应该 注意: 被调函数中对形参数椐的不恰当的改变,会破坏调用函数中的实参数据。

用数组作参数,本身就可以改变数组元素的值,不需要再返回数组,况且也不能返回数组,只能是指针。如果array是return在函数中,这在函数执行完后会回收array占用的地址。如果这时return指针的话,只是一个空地址。可以使用static是array成为static local variable,或者new(c++才有new和delete,c中用malloc,释放用free),这样函数完成时不会释放它占用的内存空间。

C++代码
  1. #include <iostream>   
  2. using namespace std;   
  3. int* foo()   
  4. {   
  5. int *arr = new int[10]; //or static int arr[10];   
  6. for (int i=0; i<10; i++)   
  7. arr[i] = i;   
  8. return arr;   
  9. }   
  10.   
  11. int main(){   
  12. int *arr=0;   
  13. arr = foo();   
  14. for (int i=0; i<10; i++)   
  15. cout << arr[i];   
  16. delete [] arr;   
  17. }  


或者通过struct, 返回struct中的array。
C代码
  1. struct mystruct {   
  2.     int vector[20];   
  3. }   
  4.   
  5. struct mystruct foo()   
  6. {   
  7.     struct mystruct bar;   
  8.   
  9.     ...do something nifty with bar...   
  10.   
  11.     return bar;   
  12. }  
 

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..

C代码
  1. typedef int array_int3[4];   
  2.   
  3. array_int3 func(...)   
  4. {   
  5. array_int3 ret={1,2,3,4};   
  6.   
  7.      return ret;   
  8. }  




数组作为函数参数举例:

1. 顺序查找
   查找是计算机中经常要遇到的一种操作,其含义是在一组数据中查找到待查数据的位置。当一组数据无序时,一般采用顺序查找。顺序查找是把给定的值与这组数据中的每个值顺序比较,如果找到,就输出这个值的位置,如果找不到则报告没有找到。下面是顺序查找的程序:


例6-24 
C代码
  1.   #include <stdio.h>    
  2. #define SIZE 10    
  3. //求给定值key的位置,找到则返回其下标,找不到返回-1    
  4. int seq_search(int v[],int n,int key) //v[]:数组参数 n:数组元素个数 key:待查的值    
  5. {    
  6.   int i;    
  7.   for(i = 0;i < n;i ++)    
  8.    if(key == v[i])    
  9.     return i;    
  10.   return -1; //没找到    
  11. }    
  12. void main(void)    
  13. {    
  14.   int d[SIZE],KEY,i,index;    
  15.   printf("Input %d numbers:/n",SIZE);    
  16.   for(i = 0;i < SIZE;i ++)    
  17.    scanf("%d",&d[i]);    
  18.   printf("Input a key you want to search:/n");    
  19.   scanf("%d",&KEY);    
  20.   index = seq_search(d,SIZE,KEY);    
  21.   if(index >= 0)    
  22.    printf("The index of the key is %d./n",index);    
  23.   else    
  24.    printf("Not found./n");    
  25. }   


运行该程序并输入:
   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 
C代码
  1.   #include <iostream.h>    
  2. #include <string.h>    
  3. #define LENGTH 80    
  4.   
  5. //反序一个字符串    
  6. void reverse(char s[])    
  7. {    
  8.   char c;    
  9.   int i,j;    
  10.   j = strlen(s) - 1;    
  11.   for(i = 0;i < j;i ++)    
  12.   {    
  13.    c = s[i];    
  14.    s[i] = s[j];    
  15.    s[j] = c;    
  16.    j --;    
  17.   }    
  18. }    
  19.   
  20. void main(void)    
  21. {    
  22.   char str[LENGTH + 1];    
  23.   cout<<"Input a string:";    
  24.   cin>>str;    
  25.   reverse(str);    
  26.   cout<<"The string is reversed:"<<str<<endl;    
  27. }  



运行该程序并输入:
   Input a string:abcd↙
  运行结果为:
   The string is reversed:dbca
   这个程序中,函数reverse没有指定数组元素个数的参数,这是因为字符串总是以一个'{content}'结束,通过'{content}'就可以得到字符数组中元素的个数,也就是字符串的长度,这里我们可以看到使用字符串结束标识的好处。


二维数组也可作为函数参数,第一维的大小可以省略,而第二维的大小不能省略。下面是一个二维数组作为函数参数的例子:


例6-26 
C代码
  1.   #include <stdio.h>    
  2. void main()    
  3. {    
  4.   void add(int x[][4], int n, int num);    
  5.   void print(int x[][4], int n);    
  6.   int a[3][4]={{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};    
  7.   int number=10;    
  8.   add(a, 3, number);    
  9.   print(a, 3);    
  10. }    
  11. void add(int x[][4], int n, int num)    
  12. {    
  13.   for(int i = 0; i<n; i++)    
  14.   for(int j = 0; j<4; j++)    
  15.   x[i][j] = x[i][j]+num;    
  16. }    
  17. void print(int x[][4], int n)    
  18. {    
  19.   for(int i = 0; i<n; i++)    
  20.   for(int j = 0; j<4; j++)    
  21.   printf("%4d/n", x[i][j]);    
  22. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值