在C ++中,如何访问多维数组中的值?

时间:2021-09-13 21:33:52

Thanks for looking!

谢谢你的期待!

Background

I am building a quadcopter on each arm of which I have placed a strip of RGB addressable LEDs. I am using an Arduino to drive the lights and the Arduino code is C++, a language I don't know very well.

我正在每个臂上构建一个四轴飞行器,我已经放置了一条RGB可寻址LED。我正在使用Arduino来驱动灯光,Arduino代码是C ++,这是一种我不太了解的语言。

Here is the first * question I posted regarding a previous problem I had with this code. It gives you more background about what exactly I am trying to do (if you are interested).

这是我发布的第一个关于我之前使用此代码遇到的问题的*问题。它为您提供了更多关于我正在尝试做什么的背景(如果您有兴趣)。

Problem

I have now properly written the array "gpsHoldArr" thanks to the answers to my first question, but I am having trouble accessing it's values.

由于我的第一个问题的答案,我现在已正确编写数组“gpsHoldArr”,但我无法访问它的值。

In the code below, I call toggleLights(gpsHoldArr[x][y]) and pass in a subarray of gpsHoldArr. The subarray should be the result of pointing to a given LED strip ([x]) and then a given step ([y]).

在下面的代码中,我调用toggleLights(gpsHoldArr [x] [y])并传入gpsHoldArr的子数组。子阵列应该是指向给定LED条带([x])然后指向给定步骤([y])的结果。

toggleLights should then iterate the array it is passed and send the value of each LED (some number from 1-6) it is on and that LED's red, green, and blue value to the console.

然后,toggleLights应该迭代它传递的数组并发送它所在的每个LED的值(1-6中的某个数字),并将LED的红色,绿色和蓝色值发送到控制台。

Unfortunately, when I run the code below, I get this error: cannot convert int(*)[3] to int* for argument 1 to 'void toggleLights(int*)'

不幸的是,当我运行下面的代码时,我收到此错误:无法将int(*)[3]转换为int *以将参数1转换为'void toggleLights(int *)'

Any help would be greatly appreciated.

任何帮助将不胜感激。

Here's the current code:

//4 arms, 6 steps, 6 leds
int gpsHoldArr[4][6][6][3] = {
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
  {
    {{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
    {{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
}
};

toggleLights(gpsHoldArr[0][0]); //Toggles lights on strip #1, step #1
toggleLights(gpsHoldArr[1][0]); //Toggles lights on strip #2, step #1
toggleLights(gpsHoldArr[2][0]); //Toggles lights on strip #3, step #1
toggleLights(gpsHoldArr[3][0]); //Toggles lights on strip #4, step #1 

void toggleLights(int lights[]){
  for(int i = 0; i <= 6; ++i)
  {
    set_color_led(i, lights[i], lights[i], lights[i]);
  } 
}

void set_color_led(int led, int r, int g, int b){
   Serial.println(led); //Which LED (or "pixel") is it?
   Serial.println(r); //What is the red value?
   Serial.println(g); //What is the green value?
   Serial.println(b); //What is the blue value? 
}

3 个解决方案

#1


1  

I don't think that toggleLights() is doing what you think it is doing. Its input is a 1-D array, but you are passing it a 2-D array of size [6][3]. When toggleLights(gpsHoldArr[0][0]); is called, the 1-D memory array that the function sees is {255,0,0,0,0,0}, i.e. the first six values in your array. Then, for each of these values you are calling set_color_led(); and passing the same value for multiple arguments. Unrolling the loop in toggleLights(), this translates to

我不认为toggleLights()正在做你认为它正在做的事情。它的输入是一维数组,但是你传递的是一个2-D大小的数组[6] [3]。当toggleLights(gpsHoldArr [0] [0]);被称为,函数看到的1-D内存数组是{255,0,0,0,0,0},即数组中的前六个值。然后,对于每个值,您调用set_color_led();并为多个参数传递相同的值。在toggleLights()中展开循环,这转换为

// set_color_led(i, lights[i], lights[i], lights[i]) for i = {0, ..., 6}
set_color_led(0, 255, 255, 255);
set_color_led(1, 0, 0, 0);
set_color_led(2, 0, 0, 0);
set_color_led(3, 0, 0, 0);
set_color_led(4, 0, 0, 0);
set_color_led(5, 0, 0, 0);
set_color_led(6, 0, 0, 0); // bug here as noted by molbdnilo

This is probably not what you want. I would change the definition of toggleLights() to the following:

这可能不是你想要的。我会将toggleLights()的定义更改为以下内容:

void toggleLights(int lights[][3]){
  for(int i = 0; i < 6; ++i)
  {
    set_color_led(i, lights[i][0], lights[i][1], lights[i][2]);
  } 
}

In this case when calling toggleLights(gpsHoldArr[0][0]);, the 2-D array that this function see is

在这种情况下,当调用toggleLights(gpsHoldArr [0] [0]);时,此函数看到的2-D数组是

{{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}

and unrolling the loop in toggleLights(), this translates to the following series of function calls:

并在toggleLights()中展开循环,这转换为以下一系列函数调用:

// set_color_led(i, lights[i][0], lights[i][1], lights[i][2]) for i = {0, ..., 5}
set_color_led(0, 255, 0, 0);
set_color_led(1, 0, 0, 0);
set_color_led(2, 0, 0, 0);
set_color_led(3, 0, 0, 0);
set_color_led(4, 0, 0, 0);
set_color_led(5, 0, 0, 0);

#2


2  

I would probably go about it like this, removing some repetition along the way.
(Macro trickery only because of Arduino - on a desktop I would use classes instead of arrays.)

我可能会这样做,在途中删除一些重复。 (宏诡计只是因为Arduino - 在桌面上我会使用类而不是数组。)

struct LED { int r, g, b; };

#define BLACK  {0, 0, 0}
#define RED    {255, 0, 0}

#define DEFAULT_LEDS \
  { {RED, BLACK, BLACK, BLACK, BLACK, BLACK},\
    {RED, RED,   BLACK, BLACK, BLACK, BLACK},\
    {RED, RED,   RED,   BLACK, BLACK, BLACK},\
    {RED, RED,   RED,   RED,   BLACK, BLACK},\
    {RED, RED,   RED,   RED,   RED,   BLACK},\
    {RED, RED,   RED,   RED,   RED,   RED}}

LED gpsHoldArr[4][6][6] = {
   DEFAULT_LEDS,
   DEFAULT_LEDS,
   DEFAULT_LEDS,
   DEFAULT_LEDS
};


void set_color_led(int index, const LED& led){
   Serial.println(index); //Which LED (or "pixel") is it?
   Serial.println(led.r); //What is the red value?
   Serial.println(led.g); //What is the green value?
   Serial.println(led.b); //What is the blue value? 
}

void toggleLights(LED (&leds)[6]){
  for(int i = 0; i < 6; ++i)  // You had a '<=' bug here.
  {
    set_color_led(i, leds[i]);
  } 
}

toggleLights(gpsHoldArr[0][0]); //Toggles lights on strip #1, step #1

#3


1  

You are passing in a 2D array when your function exists a 1D array (decayed to a pointer).

当函数存在一维数组(衰减为指针)时,您将传入二维数组。

Can I suggest eliminating array dimmensions by making structs/classes, it will make stuff much clearer.

我可以通过制作结构/类来建议消除数组dimmensions,它会使事情更清晰。

for e.g

struct Led{
    int r,g,b;
};

void toggleLights(Led lights[]){

Led gpsHoldArr[4][6][6] = 

set_color_led(i, lights[i].r, lights[i].g, lights[i].b);

That should be all the changes you need to make, the rest should work as-is.

这应该是您需要进行的所有更改,其余应该按原样进行。

You can go further and make an arm struct and a step struct as well.

您可以进一步制作arm结构和步骤结构。

#1


1  

I don't think that toggleLights() is doing what you think it is doing. Its input is a 1-D array, but you are passing it a 2-D array of size [6][3]. When toggleLights(gpsHoldArr[0][0]); is called, the 1-D memory array that the function sees is {255,0,0,0,0,0}, i.e. the first six values in your array. Then, for each of these values you are calling set_color_led(); and passing the same value for multiple arguments. Unrolling the loop in toggleLights(), this translates to

我不认为toggleLights()正在做你认为它正在做的事情。它的输入是一维数组,但是你传递的是一个2-D大小的数组[6] [3]。当toggleLights(gpsHoldArr [0] [0]);被称为,函数看到的1-D内存数组是{255,0,0,0,0,0},即数组中的前六个值。然后,对于每个值,您调用set_color_led();并为多个参数传递相同的值。在toggleLights()中展开循环,这转换为

// set_color_led(i, lights[i], lights[i], lights[i]) for i = {0, ..., 6}
set_color_led(0, 255, 255, 255);
set_color_led(1, 0, 0, 0);
set_color_led(2, 0, 0, 0);
set_color_led(3, 0, 0, 0);
set_color_led(4, 0, 0, 0);
set_color_led(5, 0, 0, 0);
set_color_led(6, 0, 0, 0); // bug here as noted by molbdnilo

This is probably not what you want. I would change the definition of toggleLights() to the following:

这可能不是你想要的。我会将toggleLights()的定义更改为以下内容:

void toggleLights(int lights[][3]){
  for(int i = 0; i < 6; ++i)
  {
    set_color_led(i, lights[i][0], lights[i][1], lights[i][2]);
  } 
}

In this case when calling toggleLights(gpsHoldArr[0][0]);, the 2-D array that this function see is

在这种情况下,当调用toggleLights(gpsHoldArr [0] [0]);时,此函数看到的2-D数组是

{{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}

and unrolling the loop in toggleLights(), this translates to the following series of function calls:

并在toggleLights()中展开循环,这转换为以下一系列函数调用:

// set_color_led(i, lights[i][0], lights[i][1], lights[i][2]) for i = {0, ..., 5}
set_color_led(0, 255, 0, 0);
set_color_led(1, 0, 0, 0);
set_color_led(2, 0, 0, 0);
set_color_led(3, 0, 0, 0);
set_color_led(4, 0, 0, 0);
set_color_led(5, 0, 0, 0);

#2


2  

I would probably go about it like this, removing some repetition along the way.
(Macro trickery only because of Arduino - on a desktop I would use classes instead of arrays.)

我可能会这样做,在途中删除一些重复。 (宏诡计只是因为Arduino - 在桌面上我会使用类而不是数组。)

struct LED { int r, g, b; };

#define BLACK  {0, 0, 0}
#define RED    {255, 0, 0}

#define DEFAULT_LEDS \
  { {RED, BLACK, BLACK, BLACK, BLACK, BLACK},\
    {RED, RED,   BLACK, BLACK, BLACK, BLACK},\
    {RED, RED,   RED,   BLACK, BLACK, BLACK},\
    {RED, RED,   RED,   RED,   BLACK, BLACK},\
    {RED, RED,   RED,   RED,   RED,   BLACK},\
    {RED, RED,   RED,   RED,   RED,   RED}}

LED gpsHoldArr[4][6][6] = {
   DEFAULT_LEDS,
   DEFAULT_LEDS,
   DEFAULT_LEDS,
   DEFAULT_LEDS
};


void set_color_led(int index, const LED& led){
   Serial.println(index); //Which LED (or "pixel") is it?
   Serial.println(led.r); //What is the red value?
   Serial.println(led.g); //What is the green value?
   Serial.println(led.b); //What is the blue value? 
}

void toggleLights(LED (&leds)[6]){
  for(int i = 0; i < 6; ++i)  // You had a '<=' bug here.
  {
    set_color_led(i, leds[i]);
  } 
}

toggleLights(gpsHoldArr[0][0]); //Toggles lights on strip #1, step #1

#3


1  

You are passing in a 2D array when your function exists a 1D array (decayed to a pointer).

当函数存在一维数组(衰减为指针)时,您将传入二维数组。

Can I suggest eliminating array dimmensions by making structs/classes, it will make stuff much clearer.

我可以通过制作结构/类来建议消除数组dimmensions,它会使事情更清晰。

for e.g

struct Led{
    int r,g,b;
};

void toggleLights(Led lights[]){

Led gpsHoldArr[4][6][6] = 

set_color_led(i, lights[i].r, lights[i].g, lights[i].b);

That should be all the changes you need to make, the rest should work as-is.

这应该是您需要进行的所有更改,其余应该按原样进行。

You can go further and make an arm struct and a step struct as well.

您可以进一步制作arm结构和步骤结构。