Pages: [1]   Go Down
Author Topic: passing arrays by reference  (Read 769 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Quote

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=(i*10);
  }
}


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
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);
  }
}
smiley
« Last Edit: October 23, 2009, 07:49:43 pm by AlphaBeta » Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);
  }
}
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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.

int x[10];
Code:
x[2]
is the same as
Code:
*(x+2*sizeof(int))

03:00
/me got to sleep! See you  smiley
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 209
Posts: 13015
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
pass an array into a function by reference

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

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

No, you wouldn't.  This...

Code:
 function(&counter[]);

...produces this...

Quote
error C2059: syntax error : ']'

...in Visual Studio 2008.


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

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


Try dropping the reference...

Code:
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);
  }
}
« Last Edit: October 23, 2009, 08:37:36 pm by bcook » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And we arrive back to reply #1  smiley

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Huntsville, Alabama, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 327
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

AlphaBeta,

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

Regards,

-Mike
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
AIf you have an array of, for instance, ints
Code:
int x[10];
And an array reference like
Code:
i = x[2];
Then the pointer equivalent would be
Code:
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.
 :-[
Logged

Pages: [1]   Go Up
Jump to: