C++用指针变量作为函数的参数接受数组的值的问题的总结

原博客地址: https://blog.csdn.net/QianShouYuZhiBo/article/details/9454477

实参和形参的四种结合方式
实参 形参 实例
数组名 数组名 1.1
数组名 指针变量 1.2
指针变量 数组名 1.3
指针变量 指针变量 1.4

本文以输入10个整数,然后对其进行排序,然后输出的程序为例:


形参为数组名,实参是数组名

实例代码1.1:

[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3. int main(){  
  4.     void Sort(int a[],int n);  
  5.     int a[10],i;  
  6.     cout<<"Please input 10 interges: "<<endl;  
  7.     for(i=0;i<10;i++){  
  8.         cin>>a[i];  
  9.     }  
  10.     Sort(a,10);  
  11.     cout<<"Sorted order:";  
  12.     for(i=0;i<10;i++){  
  13.         cout<<a[i]<<" ";  
  14.     }  
  15.     cout<<endl;  
  16.     return 0;  
  17. }   
  18. void Sort(int a[], int n){  
  19.     int i,j,k,tool;  
  20.     for(i=0;i<n;i++){  
  21.         k=i;  
  22.         for(j=i;j<n;j++){  
  23.             if(a[j]<a[k])  
  24.             k=j;   
  25.         }  
  26.         tool=a[k];  
  27.         a[k]=a[i];  
  28.         a[i]=tool;  
  29.     }  
  30. }  

形参中a[ ]中,可以不写任何的数字,只需要表示这是一个数组即可。如果其中放数字的话,可以放置任何一个正整数(不一定等于实参数组的大小,可以比实参中的数组大或者小)。

即:

[cpp]  view plain  copy
  1. void Sort(int a[], int n )  

也可以写成

[cpp]  view plain  copy
  1. void Sort(int a[2], int n)  

或者

[cpp]  view plain  copy
  1. void Sort(int a[12], int n)  


实参是数组名,形参是指针变量

实例代码1.2:

[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3. int main(){  
  4.     void Sort(int a[],int n);  
  5.     int a[10],i;  
  6.     cout<<"Please input 10 interges: "<<endl;  
  7.     for(i=0;i<10;i++){  
  8.         cin>>a[i];  
  9.     }  
  10.     Sort(a,10);  
  11.     cout<<"Sorted order:";  
  12.     for(i=0;i<10;i++){  
  13.         cout<<a[i]<<" ";  
  14.     }  
  15.     cout<<endl;  
  16.     return 0;  
  17. }   
  18. void Sort(int *a, int n){  
  19.     int i,j,k,tool;  
  20.     for(i=0;i<n;i++){  
  21.         k=i;  
  22.         for(j=i;j<n;j++){  
  23.             if(a[j]<a[k])  
  24.             k=j;   
  25.         }  
  26.         tool=a[k];  
  27.         a[k]=a[i];  
  28.         a[i]=tool;  
  29.     }  
  30. }  
在文章《 C++一维数组和指针的关系总结》中,已经提到数组名实际上代表数组的首元素的地址也就是说a等价于&a[0]

在实参中,数组名代表数组中的第一个元素的地址,所以实参实际上只传递了数组的第一个元素的指针。因此,在形参中,只需要一个指针变量来接受传递过来的值即可。

实参是指针变量,形参是数组

实例代码1.3:

[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3. int main(){  
  4.     void Sort(int a[],int n);  
  5.     int a[10],i;  
  6.     cout<<"Please input 10 interges: "<<endl;  
  7.     for(i=0;i<10;i++){  
  8.         cin>>a[i];  
  9.     }  
  10.     Sort(&a[0],10);  
  11.     cout<<"Sorted order:";  
  12.     for(i=0;i<10;i++){  
  13.         cout<<a[i]<<" ";  
  14.     }  
  15.     cout<<endl;  
  16.     return 0;  
  17. }   
  18. void Sort(int a[], int n){  
  19.     int i,j,k,tool;  
  20.     for(i=0;i<n;i++){  
  21.         k=i;  
  22.         for(j=i;j<n;j++){  
  23.             if(a[j]<a[k])  
  24.             k=j;   
  25.         }  
  26.         tool=a[k];  
  27.         a[k]=a[i];  
  28.         a[i]=tool;  
  29.     }  
  30. }  

跟上文分析的一致,通过&a[0]作为实参进行传值,证明了数组名实际上代表数组的首元素的地址也就是说a等价于&a[0]

形参是指针变量,实参是指针变量

实例代码1.4:

[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3. int main(){  
  4.     void Sort(int a[],int n);  
  5.     int a[10],i;  
  6.     cout<<"Please input 10 interges: "<<endl;  
  7.     for(i=0;i<10;i++){  
  8.         cin>>a[i];  
  9.     }  
  10.     Sort(&a[0],10);  
  11.     cout<<"Sorted order:";  
  12.     for(i=0;i<10;i++){  
  13.         cout<<a[i]<<" ";  
  14.     }  
  15.     cout<<endl;  
  16.     return 0;  
  17. }   
  18. void Sort(int *a, int n){  
  19.     int i,j,k,tool;  
  20.     for(i=0;i<n;i++){  
  21.         k=i;  
  22.         for(j=i;j<n;j++){  
  23.             if(a[j]<a[k])  
  24.             k=j;   
  25.         }  
  26.         tool=a[k];  
  27.         a[k]=a[i];  
  28.         a[i]=tool;  
  29.     }  
  30. }  

这种方法是最直接了当的方法,实参和形参的传递的数值和接受的数值都是指针


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值