将多维数组作为参数传递给函数
Pass multidimensional Array as parameter to function
我正在为 TTGO-T 编写一个基本的菜单系统,以便能够 select 在不同的功能之间切换,
我正在尝试将字符串的多维 (2D) 数组传递给函数 (drawMenu & updateMenu)。
Ex: drawMenu(TFT_GREEN, 4, options);
数组是在编译时定义的。如果我在函数
中指定哪个数组,它就会工作
示例:
void drawMenu(uint16_t color, int stringArrLength)
{
tft.fillScreen(TFT_BLACK);
tft.setTextColor(color, TFT_BLACK);
tft.drawCentreString("TTGO - T",69,0,4);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
for(int i=0;i<stringArrLength;i++)
{
if(count == i)
{
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString("> ", 0, (20 + (20 * i)), 2);
tft.setTextColor(TFT_WHITE, color);
tft.drawString(options[i], 10, (20 + (20 * i)), 2);
}
else
{
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString(options[i], 5, (20 + (20 * i)), 2);
}
}
}
...但我希望能够有子菜单,而不必为每个子菜单编写新功能。编译时出现此错误
cannot convert 'char (*)[25]' to 'char**' for argument '3' to 'void
drawMenu(uint16_t, int, char**)'
我还以为只要在参数里定义成多维数组就可以了,我可能就是傻了。
Code:
#include <TFT_eSPI.h>
#include <SPI.h>
#include <Wire.h>
TFT_eSPI tft = TFT_eSPI();
int leftButton = 0;
int rightButton = 35;
int count = 0;
/**
* NOTE: If adding more options dont forget the change the parameters for
* drawMenu();
* &
* updateMenu();
*/
char options[4][25] = {
"Triangle",
"TicTacToe",
"Option 3",
"Change Color"
};
char colors[6][25] = {
"Red",
"Orange",
"Yellow",
"Green",
"Blue",
"Purple"
};
void setup(void)
{
pinMode(leftButton,INPUT_PULLUP);
pinMode(rightButton,INPUT_PULLUP);
tft.init();
tft.fillScreen(TFT_BLACK);
drawMenu(TFT_GREEN, 4, options);
}
void loop()
{
updateMenu(4, options);
}
void drawMenu(uint16_t color, int stringArrLength, char **Arr)
{
tft.fillScreen(TFT_BLACK);
tft.setTextColor(color, TFT_BLACK);
tft.drawCentreString("TTGO - T",69,0,4);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
for(int i=0;i<stringArrLength;i++)
{
if(count == i)
{
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString("> ", 0, (20 + (20 * i)), 2);
tft.setTextColor(TFT_WHITE, color);
tft.drawString(Arr[i], 10, (20 + (20 * i)), 2);
}
else
{
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString(Arr[i], 5, (20 + (20 * i)), 2);
}
}
}
void updateMenu(int stringArrLength, char **Arr)
{
/**
* Dont need to mess with anything here
*/
if(digitalRead(leftButton) == 0 && count <= stringArrLength)
{
if(count == (stringArrLength - 1))
{
count = 0;
}
else
{
count++;
}
drawMenu(TFT_GREEN, 4, Arr);
delay(500);
}
/**
* If right button pressed call function that matches current index(count)
*
* NOTE: first "option" in options Array is at index 0; or Count = 0,
* the last option is at (array length - 1); b/c of string ending null char ([=11=]).
*/
else if(digitalRead(rightButton) == 0)
{
if(count == 0)// 1st option...
{
triangles();
delay(5000);
}
else if(count == 1)// 2nd option...
{
}
else if(count == 2)
{
}
drawMenu(TFT_GREEN, 4, Arr);// return back to menu after call
}
}
将数组传递给函数时发生的array-to-pointer转换仅适用于最外层的数组维度。在您的情况下,这意味着 char [4][25]
衰减为 char (*)[25]
,而不是 char **
,因此参数类型不兼容。
将函数参数的声明从 char **Arr
更改为 char Arr[][25]
。
我正在为 TTGO-T 编写一个基本的菜单系统,以便能够 select 在不同的功能之间切换,
我正在尝试将字符串的多维 (2D) 数组传递给函数 (drawMenu & updateMenu)。
Ex: drawMenu(TFT_GREEN, 4, options);
数组是在编译时定义的。如果我在函数
中指定哪个数组,它就会工作
示例:
void drawMenu(uint16_t color, int stringArrLength)
{
tft.fillScreen(TFT_BLACK);
tft.setTextColor(color, TFT_BLACK);
tft.drawCentreString("TTGO - T",69,0,4);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
for(int i=0;i<stringArrLength;i++)
{
if(count == i)
{
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString("> ", 0, (20 + (20 * i)), 2);
tft.setTextColor(TFT_WHITE, color);
tft.drawString(options[i], 10, (20 + (20 * i)), 2);
}
else
{
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString(options[i], 5, (20 + (20 * i)), 2);
}
}
}
...但我希望能够有子菜单,而不必为每个子菜单编写新功能。编译时出现此错误
cannot convert 'char (*)[25]' to 'char**' for argument '3' to 'void drawMenu(uint16_t, int, char**)'
我还以为只要在参数里定义成多维数组就可以了,我可能就是傻了。
Code:
#include <TFT_eSPI.h>
#include <SPI.h>
#include <Wire.h>
TFT_eSPI tft = TFT_eSPI();
int leftButton = 0;
int rightButton = 35;
int count = 0;
/**
* NOTE: If adding more options dont forget the change the parameters for
* drawMenu();
* &
* updateMenu();
*/
char options[4][25] = {
"Triangle",
"TicTacToe",
"Option 3",
"Change Color"
};
char colors[6][25] = {
"Red",
"Orange",
"Yellow",
"Green",
"Blue",
"Purple"
};
void setup(void)
{
pinMode(leftButton,INPUT_PULLUP);
pinMode(rightButton,INPUT_PULLUP);
tft.init();
tft.fillScreen(TFT_BLACK);
drawMenu(TFT_GREEN, 4, options);
}
void loop()
{
updateMenu(4, options);
}
void drawMenu(uint16_t color, int stringArrLength, char **Arr)
{
tft.fillScreen(TFT_BLACK);
tft.setTextColor(color, TFT_BLACK);
tft.drawCentreString("TTGO - T",69,0,4);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
for(int i=0;i<stringArrLength;i++)
{
if(count == i)
{
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString("> ", 0, (20 + (20 * i)), 2);
tft.setTextColor(TFT_WHITE, color);
tft.drawString(Arr[i], 10, (20 + (20 * i)), 2);
}
else
{
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString(Arr[i], 5, (20 + (20 * i)), 2);
}
}
}
void updateMenu(int stringArrLength, char **Arr)
{
/**
* Dont need to mess with anything here
*/
if(digitalRead(leftButton) == 0 && count <= stringArrLength)
{
if(count == (stringArrLength - 1))
{
count = 0;
}
else
{
count++;
}
drawMenu(TFT_GREEN, 4, Arr);
delay(500);
}
/**
* If right button pressed call function that matches current index(count)
*
* NOTE: first "option" in options Array is at index 0; or Count = 0,
* the last option is at (array length - 1); b/c of string ending null char ([=11=]).
*/
else if(digitalRead(rightButton) == 0)
{
if(count == 0)// 1st option...
{
triangles();
delay(5000);
}
else if(count == 1)// 2nd option...
{
}
else if(count == 2)
{
}
drawMenu(TFT_GREEN, 4, Arr);// return back to menu after call
}
}
将数组传递给函数时发生的array-to-pointer转换仅适用于最外层的数组维度。在您的情况下,这意味着 char [4][25]
衰减为 char (*)[25]
,而不是 char **
,因此参数类型不兼容。
将函数参数的声明从 char **Arr
更改为 char Arr[][25]
。