Pages: [1]   Go Down
Author Topic: Syntax for arrays as operands  (Read 377 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the syntax in order to pass an array that is not predefined in a function as operand?

For example i have this function:

    void kilos(int percentage[2],char liquid[2][4]);

So  if i make 2 different arrays(declare and define) and pass them as operands everything work properly.

How can this work if i do not want to make the arrays before the call of the function?
If i have the variables i want to pass and i do not have a and also do not want to make the arrays before i call the function.How can i do it all at once?

Is the only solution to analyze the operands and not use arrays?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How can this work if i do not want to make the arrays before the call of the function?
It can't. If the function expects an argument, you must pass it one. That means you need to have one to pass it.

Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is want i guess. smiley-sad so there is no way that i could declare(this can happen) and initialize the arrays as i call the function inside the parenthesis.Something like:

kilos(int percent[2]={40,60},char liq[2][4]={'wate','milk'})
« Last Edit: March 20, 2013, 07:51:58 am by kyrpav » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so there is no way that i could declare(this can happen) and initialize the arrays as i call the function inside the parenthesis.
No.
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not 100% sure what you're asking but you can write overriding functions with the same name that expect different parameters.

Code:
void setup() {
  func(1,2);
  func();
}

void loop() {
}

void func(int a, int b) {
 // do stuff with a & b
}

void func() {
   // nothing
}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is want i guess. smiley-sad so there is no way that i could declare(this can happen) and initialize the arrays as i call the function inside the parenthesis.Something like:

No, but you could define a local or global const array containing the values and use that as the argument.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: