[英]Arduino/C++ An array of pointers with reference
I have an array:我有一个数组:
#define SELECT_KEY 1
char *menu_main[] = { "Individual", "OFF", "ON", "Initialise", "Scan", "Read"};
char *individ_sub_menu[] = { "item1", "Item2" };
char *inview_menu = &menu_main;
int selectPos = 0;
int getKeyPress()
{
//for purpose of this example
return 1;
}
void updateMenu()
{
uint8_t uiKeyCode = getKeyPress();
if (uiKeyCode == SELECT_KEY && inview_menu[selectPos] == "Individual")
{
inview_menu = &individ_sub_menu
}
}
So I'm getting an issue: error: cannot convert 'char* (*)[6]' to 'char*' in initialization所以我遇到了一个问题:错误:在初始化中无法将 'char* (*)[6]' 转换为 'char*'
Was hoping that I can switch the reference between those two arrays of char pointers and access the contents based on that inview menu希望我可以在字符指针的这两个 arrays 之间切换引用并根据该 inview 菜单访问内容
char *menu_main[]
is an array of pointers to char
. char *menu_main[]
是一个指向char
的指针数组。
&menu_main
is a pointer to an array of pointers to char
. &menu_main
是指向char
指针数组的指针。
char *inview_menu
is a single pointer to char
. char *inview_menu
是指向char
的单个指针。
Obviously these are incompatible types.显然这些是不兼容的类型。
You probably want char **inview_menu = menu_main;
你可能想要
char **inview_menu = menu_main;
(Note that an array decays to a pointer in expressions) (请注意,数组衰减为表达式中的指针)
Also, C++ forbids casting string literals to non-const char*
.此外, C++ 禁止将字符串文字转换为非常量
char*
。 So use const char*
.所以使用
const char*
。
const char *menu_main[] = { "Individual", "OFF", "ON", "Initialise", "Scan", "Read"};
const char *individ_sub_menu[] = { "item1", "Item2" };
const char **inview_menu = menu_main;
Those two arrays have two different types:这两个 arrays 有两种不同的类型:
char *menu_main[] = { "Individual", "OFF", "ON", "Initialise", "Scan", "Read"};
char *individ_sub_menu[] = { "item1", "Item2" };
menu_main
got type char *[6]
and individ_sub_menu
got type char *[2]
. menu_main
得到了char *[6]
类型, individ_sub_menu
得到了char *[2]
类型。 You can't pass both through same interface unless you erase that array type, which can be done by using a pointer to first element of array.除非您删除该数组类型,否则您不能通过相同的接口传递两者,这可以通过使用指向数组第一个元素的指针来完成。 You have to pass\store size of array separately:
您必须分别传递\存储数组的大小:
const char *menu_main[] = { "Individual", "OFF", "ON", "Initialise", "Scan", "Read"};
const char *individ_sub_menu[] = { "item1", "Item2" };
struct menu {
unsigned item_count;
const char **items;
} inview_menu = { sizeof(menu_main)/sizeof(menu_main[0]) , menu_main };
You can create both menus beforehand and pass one or another as a struct witha shallow copy (assuming menu item strings do not change).您可以预先创建两个菜单并将一个或另一个作为具有浅拷贝的结构传递(假设菜单项字符串不会更改)。
menu_main
is an array of pointers, that much you realise. menu_main
是一个指针数组,你意识到这一点。
You seem to think that &menu_main
is a reference to menu_main
but that is incorrect.您似乎认为
&menu_main
是对menu_main
的引用,但这是不正确的。 &menu_main
is the address of menu_main
and &
is the address-of operator. &menu_main
是menu_main
的地址, &
是操作符的地址。 It's confusing because the same symbol &
in a different context does mean a reference.这很令人困惑,因为相同的符号
&
在不同的上下文中确实意味着引用。 However you may not be using reference in the technical sense that I am.但是,您可能没有像我那样使用技术意义上的参考。
In any case it doesn't matter, what you have is an array, and you can easily use a pointer to refer to an array, like this无论如何都没关系,你拥有的是一个数组,你可以很容易地使用一个指针来引用一个数组,就像这样
char *menu_main[] = { "Individual", "OFF", "ON", "Initialise", "Scan", "Read"};
char *individ_sub_menu[] = { "item1", "Item2" };
char **inview_menu = menu_main;
void updateMenu()
{
uint8_t uiKeyCode = getKeyPress();
if (uiKeyCode == SELECT_KEY && inview_menu[selectPos] == "Individual")
{
inview_menu = individ_sub_menu;
}
}
Note that because you have an array of pointers, inview_menu
is a pointer to a pointer (aka a double pointer).请注意,因为您有一个指针数组,
inview_menu
是指向指针(也称为双指针)的指针。 Also note that the address-of operator &
is not needed in this code.另请注意,此代码中不需要操作符
&
的地址。
EDIT Also note that inview_menu[selectPos] == "Individual"
is incorrect.编辑还要注意
inview_menu[selectPos] == "Individual"
是不正确的。 You cannot use ==
to compare C strings, you should use the strcmp
function instead.您不能使用
==
来比较 C 字符串,您应该使用strcmp
function 代替。
if (uiKeyCode == SELECT_KEY && strcmp(inview_menu[selectPos], "Individual") == 0)
Stop using raw c-arrays in c++ code, and it gets so much easier (note that you should do something different than your "define", but I have kept it):停止在 c++ 代码中使用原始 c 数组,它变得容易多了(请注意,您应该做一些与“定义”不同的事情,但我保留了它):
#include <vector>
#include <string>
using namespace std;
#define SELECT_KEY 1
vector<string> menu_main = { "Individual", "OFF", "ON", "Initialise", "Scan", "Read"};
vector<string> individ_sub_menu = { "item1", "Item2" };
vector<string>* inview_menu = &menu_main;
int selectPos = 0;
int getKeyPress()
{
//for purpose of this example
return 1;
}
void updateMenu()
{
uint8_t uiKeyCode = getKeyPress();
if (uiKeyCode == SELECT_KEY && (*inview_menu)[selectPos] == "Individual")
{
inview_menu = &individ_sub_menu;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.