Go Down

Topic: passing arrays by reference (Read 820 times) previous topic - next topic

Coding Badly

#5
Oct 24, 2009, 03:36 am Last Edit: Oct 24, 2009, 03:37 am by bcook Reason: 1
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: [Select]
 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: [Select]
void function(int &functionary[]);
error C2234: 'functionary' : arrays of references are illegal



Try dropping the reference...

Code: [Select]
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);
 }
}

AlphaBeta

And we arrive back to reply #1  :)

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

rigg

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

Mike Murdock

AlphaBeta,

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

Regards,

-Mike

AlphaBeta

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

Go Up