数组作为形参

在函数传递值的过程中,一般都是值传递但是数组确实传递的指针,这样主要用于避免过多数据复制占用过多的内存空间。
我们在设置函数的时候可以使用
 int puta(const int geta[],int s_a) 来避免原始数组受到修改,
如此的话修改会报错
input.cpp: In function ‘int puta(const int*, int)’:
input.cpp:49:11: error: assignment of read-only location ‘*(geta + ((sizetype)(((long unsigned int)i) * 4ul)))’
   geta[i] = ipu;
下面是一个相关的例子
这里使用函数传入值,同时检测输入的值是否合法


8 #include
  9 using namespace std;
 10 
 11 int puta(int *geta,int s_a);
 12 
 13 int main(void)
 14 {
 15     int geta[10];
 16 //  puta(geta,sizeof(geta)/sizeof(int));
 17     if(!(puta(geta,sizeof(geta)/sizeof(int))))
 18     {
 19         for(int i = 0;i<sizeof(geta) sizeof(int);i++)  20         {
 21             cout<<geta[i]<<endl;
 22         }
 23     }
 24     else
 25     {
 26         cout<<"error input!\n";
 27     }
 28 
 29 }
 30 
 31 int puta(int geta[],int s_a)
 32 {
 33     int i;
 34     int ipu;
 35     for(i=0;i<s_a;i++)
 36     {
 37         if(!(cin>>ipu)) //cin>>check ipu data is adjust return true else return flase
 38         {
 39             cin.clear(); //clear cin flag
 40             while(cin.get() !='\n') //clear stdout buffer
 41                 continue;
 42             cout<<"error input!\n";
 43             return 1;
 44         }
 45         else if (ipu<0)
 46         {
 47             return 1;
 48         }
 49         geta[i] = ipu;
 50     }
 51     return 0;
 52 }    
</s_a;i++)
</geta[i]<<endl;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7728585/viewspace-2106201/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7728585/viewspace-2106201/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值