ITPub博客

首页 > 应用开发 > IT综合 > 【C++】数组指针与error: lvalue required as increment operand

【C++】数组指针与error: lvalue required as increment operand

原创 IT综合 作者:风尘_NULL 时间:2017-07-28 12:26:17 0 删除 编辑
int (*p)[10]是一个数组指针,但是这个指针不是指向数组的首地址,而是指向数组名的地址。
using arrT=int[10];
arrT
* functions(arrT* arrs,size_t a_size){
 int *p=(*arrs);
 for(int i=0;i<a_size;i++){
 // std::cout<<i<<std::endl;
 //正确的方式 
 *p=i;
 p++;
 //不能用下面的语句,(*arrs)++会报lvalue required as increment operand错误,因为(*arrs)不是一个左值
 //*(*arrs)=i;
 //(*arrs)++;
 
 }
 return arrs;

}

int main()
{
 int (*p)[10];
 int arrs[]={1,2,3,4,5,0,0,0,0,0};
 //不能是p=functions(arrs,10),因为是函数需要传一个数组指针而不是首地址指针
 p=functions(&arrs,10);
 for(auto c:(*p)){
 std::cout<<c<<std::endl;
 }
}

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

请登录后发表评论 登录
全部评论
腾讯科技(深圳)有限公司 DBA

注册时间:2015-04-24

  • 博文量
    76
  • 访问量
    238216