簡體   English   中英

嘗試將二維數組傳遞給 function 並出現編譯器錯誤

[英]Trying to pass 2D array to function and getting a compiler error

我是 C/C++ 初學者,正在嘗試構建一個簡單的腳本。 我在 Arduino 上運行它。

#include <Arduino.h>

int pin_locations[3][3] = {
  {8,  5, 4},
  {9,  6, 3},
  {10, 7, 2}
};

void setup() {
  for (int i=0; i<3; ++i) {
    for (int j=0; j<3; ++j) {
      pinMode(pin_locations[i][j], OUTPUT);
    }
  }
}

void convert_drawing_to_LEDS(int drawing[]) {
  for (int i=0; i<3; ++i) {
    for (int j=0; j<3; ++j) {
      if (drawing[i][j] == 1) {
        digitalWrite(pin_locations[i][j], HIGH);
      }
    }
  }
}

void loop() {
  // drawing
  int my_drawing[3][3] = {
    {1, 1, 1},
    {1, 0, 1},
    {1, 1, 1}
  };

  convert_drawing_to_LEDS(my_drawing);

}

但它給了我兩個錯誤:

src/main.cpp: 在 function 'void convert_drawing_to_LEDS(int*)': src/main.cpp:31:23: error: invalid types 'int[int]' for array subscript if (drawing[i][j] = = 1) { ^ src/main.cpp: 在 function 'void loop()': src/main.cpp:46:37: error: cannot convert 'int ( )[3]' to 'int ' for argument '1 ' 到 'void convert_drawing_to_LEDS(int*)'
convert_drawing_to_LEDS(my_drawing); ^ Compiling.pio/build/uno/FrameworkArduino/WInterrupts.c.o *** [.pio/build/uno/src/main.cpp.o] 錯誤

誰能幫我?

謝謝!

您已聲明convert_drawing_to_LEDS以采用非二維數組的int [] 這會導致參數與其在 function 中的使用方式不匹配,以及傳入的實際參數與 function 所期望的不匹配。

你反而想要:

void convert_drawing_to_LEDS(int drawing[3][3]) {

暫無
暫無

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

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