简体   繁体   中英

Arduino or c++ working with array in parameter of function

void setup() {
  Serial.begin(9600);
  printArr([64,67]);
}

void loop() {}

void printArr(byte nilai[]) {
  for (byte c = 0; c < length(nilai); c++) {
    Serial.println(nilai[c]);
  }
}

I expect the program print char of number 64 and 67 in ASCII since it was iterated in array, but instead the compiler give me error

C:\Users\User\Documents\Arduino\sketch_apr18a\sketch_apr18a.ino:10:24: note: suggested alternative: 'long'
   for (byte c = 0; c < length(nilai); c++) {
                        ^~~~~~
                        long
exit status 1
expected identifier before numeric constant

A solving can be:

void setup() {
  Serial.begin(9600);
  byte arr [] = {64,67};
  printArr(arr, 2);
}

void loop() {}

void printArr(byte *nylai, unsigned d) {
  for (byte c = 0; c < d; c++) {
    Serial.println(*(nylai + c * sizeof(byte)));
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM