# passing arrays by reference

**URL:** <https://forum.arduino.cc/t/passing-arrays-by-reference/42658>\
**Category:** Syntax & Programs\
**Created:** [October 23, 2009, 6:39pm UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658 "2009-10-23T18:39:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 23, 2009, 6:39pm UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658/1 "2009-10-23T18:39:42Z")

</div>

I am new to Arduino, but come from a c++ back ground in microsofts VS. I am trying to pass an array into a function by reference, so that more than one function can operare on the same array. An example of what I'm trying to do:

> void function(int &);
> 
> void **setup** (){  
> Serial.begin(9600);
> 
> };
> 
> void **loop** (){  
> int counter[10];  
> funtion(&cunter[]);  
> for(int i=0;i\<10;i++){  
> Serial.println(counter\*);\*
> 
> - }\*  
> _};_  
> _void function(int &functionary[]){_
> - for(int i=0;i\<10;i++){\*  
> _funtionary\_=(i10);\_  
> \__ }_\_  
> \__}_\_  
> \__[/quote]\*\_  
> _At least in VS. this is how I would do it, however arduino returns an error stating 'declaration of 'ary' as array of references'. I'm not really sure what to do as a work around. So far I have concluded that the prototype is needed to fix a bug in arduino's auto-prototyping, and I have found several postings saying you can not return an array from a function. Anything will help._  
> _rigg_

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 23, 2009, 7:17pm UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658/2 "2009-10-23T19:17:55Z")

</div>

Arrays (which actually are pointers) pass by reference by default.

```cpp
void function(int arr[]);

void setup(){
  Serial.begin(9600);
  
}

void loop(){
  int counter[10];
  funtion(cunter);
  for(int i=0;i<10;i++){
    Serial.println(counter[i]);
  }
}

void function(int arr[]){
  for(int i=0;i<10;i++){
    arr[i]=(i*10);
  }
}

```

🙂

---

<div class="post-metadata">

**Author:** ![GrooveFlotilla](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grooveflotilla/32/213680_2.png) [@GrooveFlotilla](https://forum.arduino.cc/u/GrooveFlotilla)\
**Post date:** [October 23, 2009, 7:53pm UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658/3 "2009-10-23T19:53:18Z")

</div>

I corrected the spelling, the syntax and removed the extraneous semicolons:

```cpp
const int arraySize = 10;
void setup(){
  Serial.begin(9600);
}

void loop(){
  int counter[arraySize];
  function(counter);
  for(int i=0;i<arraySize;i++){
    Serial.println(counter[i]);
  }
}

void function(int *functionary){
  for(int i=0; i < arraySize; ++i){
    functionary[i]=(i*10);
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 24, 2009, 12:35am UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658/4 "2009-10-24T00:35:14Z")

</div>

Thank you Groove i know there are some spelling mistakes in the code. This is just a quick example of my code to clearly demonstrate the problem i was having.

AlphaBata- do the arrays pass by reference because of arduino's auto-prototype thing. Because I tryed just running code very similer to how you have it writen and did not get anything to print to the serial line. Should I leave off the prototype?

Thank you for your quick responce.

rigg

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 24, 2009, 1:00am UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658/5 "2009-10-24T01:00:04Z")

</div>

Because arrays are pointers, you're passing arrays by 'reference'.

You'll have to excuse my code, I was too quick on the Post button. It should be OK now.

`int x[];` and `int *x;` are basically the exact same.

C++ can never pass an array by value, because of the nature of how arrays work.  
The variable is just a pointer to the address of the first entry.

```cpp
int x[10];

```

`x[2]` is the same as `*(x+2*sizeof(int))`

03:00  
/me got to sleep! See you 🙂

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [October 24, 2009, 1:36am UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658/6 "2009-10-24T01:36:02Z")

</div>

> pass an array into a function by reference

In C(++), that isn't necessary. Arrays are always passed by reference.

> At least in VS. this is how I would do it

No, you wouldn't. This...

```arduino
  function(&counter[]);

```

...produces this...

> error C2059: syntax error : ']'

...in Visual Studio 2008.

If you change to a prototype with a named argument this is the result...

```arduino
void function(int &functionary[]);
error C2234: 'functionary' : arrays of references are illegal

```

Try dropping the reference...

```arduino
void function(int functionary[]);

void setup(){
  Serial.begin(9600);
  
};

void loop(){
  int counter[10];
  function(counter);
  for(int i=0;i<10;i++){
    Serial.println(counter[i]);
  }
};

void function(int functionary[]){
  for(int i=0;i<10;i++){
    functionary[i]=(i*10);
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 24, 2009, 11:45am UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658/7 "2009-10-24T11:45:25Z")

</div>

And we arrive back to reply [#1](http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1256323182/5#1) 🙂

rigg: Have you solved the problem? Got some code that does what it's supposed to do?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 24, 2009, 6:24pm UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658/8 "2009-10-24T18:24:53Z")

</div>

Yes I beleive all the input has helped me solve my problem. It is going to be a few days before I can run the code to a microcontroller, however all the compiling errors have gone away. I will let every one know how it turns out in a couple of days.

Thank you to everybody how took time to help out, this is my first time asking for help in a forum and I am very thankfull with the quick responses.

rigg

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 25, 2009, 2:46am UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658/9 "2009-10-25T02:46:27Z")

</div>

AlphaBeta,

If you have an array of, for instance, ints`int x[10];`And an array reference like`i = x[2];`Then the pointer equivalent would be`i = *(x + 2);`There's no need to multiply by sizeof(int) -- C does this for you automatically.

Regards,

-Mike

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 25, 2009, 12:05pm UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658/10 "2009-10-25T12:05:28Z")

</div>

> AIf you have an array of, for instance, ints`int x[10];`And an array reference like`i = x[2];`Then the pointer equivalent would be`i = *(x + 2);`There's no need to multiply by sizeof(int) -- C does this for you automatically.

Ah. True that.

I just came from a C code dealing with void pointers.  
:-[

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 8:06pm UTC](https://forum.arduino.cc/t/passing-arrays-by-reference/42658/11 "2021-05-06T20:06:08Z")

</div>


