簡體   English   中英

Arduino 或 c++ 在 function 的參數中使用數組

[英]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]);
  }
}

我希望程序在 ASCII 中打印數字 64 和 67 的字符,因為它是在數組中迭代的,但是編譯器卻給我錯誤

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

解決方案可以是:

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)));
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM