如果[]是一个数组,+ 1意味着什么? [英] What does a+1 means if a [ ] is an array ?

查看:368
本文介绍了如果[]是一个数组,+ 1意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码是这样的:



the code is like this :

int sumr(int n , int a[])
{
  if (n==1)
return a[0];
  else return a[0]+ sumr(n-1 , a+1 );
}





最后一行a + 1?



我尝试了什么:



我认为这意味着下一个号码或者什么......但是我不确定......



the a+1 in the last line ??

What I have tried:

I think it means the next number or something ... but I'm not sure ...

推荐答案

数组的名称是指向数组第一个元素的指针,因此向它添加一个元素会使它前进第二个元素。

基本上,这是一个遍历数组并添加每个元素的坏方法。

这不需要递归,一个简单的循环就是更高效,更易于阅读和理解。
The name of an array is a pointer to the first element of the array so adding one to it will advance it to the second element.
Basically, it's a bad way to traverse an array and add each element.
That doesn't need to be recursive, a simple loop would be a lot more efficient, as well as easier to read and understand.


a 是第一个数组项的地址。 />
a + 1 是第二个数组项的地址(即& a [1] )。



该函数以递归方式传递增加的索引和数组的其余部分。
a is the address of the first array item.
a+1 is the address of the second array item (i.e. &a[1]).

The function works recursively passing the increased index and the remaining part of the array.


这篇关于如果[]是一个数组,+ 1意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆