passing array as parameters

hello i am running this test code:

boolean requestAccepted = false;
uint8_t startComunication[] = {0x81, 0x12, 0xF1, 0x81, 0x05};
uint8_t startComunication_accepted[] = {0x80, 0xF1, 0x12, 0x03, 0xC1, 0xEA, 0x8F, 0xC0};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  if (sendRequest(startComunication, startComunication_accepted)) {
    Serial.println("made");
  }

}

void loop() {
  // put your main code here, to run repeatedly:

}


boolean sendRequest(uint8_t toSend[], uint8_t toReceive) {
  for (int i = 0; i < sizeof(toSend); i++) {
    Serial.println(toSend[i], HEX);
  }
  return true;
}

i would like to pass some array as parameter to a function but looks like it doesn’t work? any ide how i could do it?

boolean sendRequest(uint8_t toSend[], uint8_t toReceive)

You could at least be consistent in how you write the definitions of the parameters to the function.

However, that is not the main problem. Put the square brackets on the second parameter and try again. It still won't work the way you want but you will at least get some output.

When passed to functions arrays are passed as pointers hence use of sizeof() in the function is a waste of time as you will get the pointer size not the array size. Incidentally, sizeof() would not have worked anyway because it returns the number of bytes used by the array, not the size of the array. You need to pass the size of the array to the function as well as the actual array.

uint8_t startComunication[] = {0x81, 0x12, 0xF1, 0x81, 0x05};
uint8_t startComunication_accepted[] = {0x80, 0xF1, 0x12, 0x03, 0xC1, 0xEA, 0x8F, 0xC0};

void setup() {
  Serial.begin(9600);
  if (sendRequest(startComunication, sizeof(startComunication), startComunication_accepted, sizeof(startComunication_accepted))) {
    Serial.println("made");
  }
}

void loop() {}

boolean sendRequest(uint8_t toSend[], uint8_t sendLen, uint8_t toReceive[], uint8_t receiveLen) {
  for (byte i = 0; i < sendLen; i++) {
    Serial.println(toSend[i], HEX);
  }
  for (byte i = 0; i < receiveLen; i++) {
    Serial.println(toReceive[i], HEX);
  }
  return true;
}

When passed to functions arrays are passed as pointers hence use of sizeof() in the function is a waste of time as you will get the pointer size not the array size. Incidentally, sizeof() would not have worked anyway because it returns the number of bytes used by the array, not the size of the array. You need to pass the size of the array to the function as well as the actual array.

to be honest i knew that it will have worked but i wanted to understand why, thanks for your explanation and thanks to Whandall too

also using pointer it works good:

uint8_t startCom[] = {0x81, 0x12, 0xF1, 0x81, 0x05};
uint8_t startCom_ok[] = {0x80, 0xF1, 0x12, 0x03, 0xC1, 0xEA, 0x8F, 0xC0};


void setup() {
  Serial.begin(9600);
  if (sendRequest(startCom, startCom_ok)) {
    Serial.println("made");
  }
}

boolean sendRequest(uint8_t *toSend, uint8_t *toReceive) {
  for (int i = 0; i < 5; i++) {
    Serial.println(*toSend, HEX);
    *toSend++;
  }
  return true;
}

void loop() {}

any advice? or maybe i should avoid to use them?
maybe the biggest problem is that now i don’t know the size of the array

aster94: any advice?

UKHeliBob: You need to pass the size of the array to the function as well as the actual array.

In a function declaration array[] is equivalent to *array.

If you need to determine the number of elements in an array, you will often see this macro used:

#define ELEMENTS(x)    (sizeof(x) / sizeof(x[0]))
// some code....

int myArray[25];     // The array must be in scope to work correctly...

// some more code...

   for (int i = 0; i < ELEMENTS(myArray); i++ ) {

      // the loop statement block...

   }

The nice thing about the macro is that it works with any data type (e.g., char, int, float, etc.)

Also, when passing an array to a function, these function signatures behave the same:

void myFunction(int array1[]) {                             // Array subscripting
   int j = array1[0];         // Assign j the first value
   int j = *array1;           // Assign j the first value
 }

// or

void myFunction(int *array) {                               // Pointer 
   int j = array1[0];         // Assign j the first value
   int j = *array1;           // Assign j the first value
}

Also, when passing an array to a function, these function signatures behave the same:

The same as in neither one will compile?

It wasn't meant to compile. It was meant to show that the parameter being passed in can be accessed either using subscripting or dereferencing. If you want it to compile, place it in a "real" program and change the variable names in the definitions. Since the function doesn't do anything, it could be that the compiler would optimize it away as it stands; I didn't check.

Thanks econjack, i should use more macro sometimes they are very useful!