Passing array to function

Status
Not open for further replies.

urbanspaceman

Well-known member
Hi, i'm stuck with a (maybe) simple problem

i have a sketch like this (simplified version)

Code:
void myfunction(int lenght, uint16_t x, uint16_t y, uint16_t s, char t_name, uint16_t color, int myarray[]) {

  for (int i = 0; i < 16; i++) {
             if(myarray[i] == 1){
              //do something
             }else{
                if (lenght < i + 1) {
                  //do another thing
                } else {
                  //or another
                }
              }
  }
}

  void setup() {
    // put your setup code here, to run once:


  }

  void loop() {
    // put your main code here, to run repeatedly:
    int my_array = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0};


   Myfunction(14, 30, 60, 18, 'A', ILI9341_ORANGE, my_array);


  }

i have a problem passing the array to the function
 
Code:
    int my_array = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0};

That is not an array. This is:
Code:
    int my_array[16] = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0};

It also helps to spell the name of the function the same way. Myfunction and myfunction aren't the same.

Pete
 
If you don't know (or are too lacy to count) you could do
Code:
int array[]  = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0};
and then determine the size by
Code:
int n = sizeof(array)/sizeof(array[0]);

As you typically pass your array by pointer, you should pass also the length of the array
Code:
myFunction(array,n);

Edit: as it seems that sizeof of an operator and not a function
Code:
int n = sizeof array /sizeof array[0] ;
is sufficient (or more appropriate?)
 
Last edited:
Status
Not open for further replies.
Back
Top