当 while(1) 被移除时指向指针比较的指针中断 - 为什么?
Pointer to pointer comparision breaks when while(1) removed - why?
尝试构建菜单系统,但 运行 遇到一些指针问题 - 我没有太多经验。
我不明白为什么删除 while(1) 会使 mainmenu_table[1][i] == &option6 之间的比较失败,但出于某种原因确实如此。
我做错了什么?使用 visual studio 和 atmega328p。谢谢
原始代码的串行输出:
Serial begins
MeNu6
MeNu6
Starting compare loop
it worked
删除了 while(1) 的串行输出。
Serial begins
MeNu6
MeNu6
Starting compare loop
the end
原始代码(包含 while(1))
const char option1[] PROGMEM = "Menu1";
const char option2[] PROGMEM = "MEnu2";
const char option3[] PROGMEM = "MeNu3";
const char option4[] PROGMEM = "Menu4";
const char option5[] PROGMEM = "MEnu5";
const char option6[] PROGMEM = "MeNu6";
const char option7[] PROGMEM = "menu7";
const char* const submenu1_table[] PROGMEM = { option1, option2, option3 }; // array of pointers to chars stored in flash
const char* const submenu2_table[] PROGMEM = { option4, option5, option6, option7 };
const char** const mainmenu_table[] PROGMEM = { submenu1_table, submenu2_table }; //array of pointers to pointers to chars in flash
// The setup() function runs once each time the micro-controller starts
void setup()
{
Serial.begin(9600);
delay(100);
Serial.println("Serial begins");
Serial.println((const __FlashStringHelper*)(mainmenu_table[1][2])); // prints "Menu6" as expected
Serial.println((const __FlashStringHelper*)option6); // also prints "Menu6"
Serial.println("Starting compare loop");
for (int i = 0; i < 4; i++) {
if ( mainmenu_table[1][i] == &option6 ) { //
Serial.println("it worked");
while (1); // COMMENTING THIS OUT MEANS DOESN'T COMPARE SUCCESSFULLY.
}
}
Serial.println("the end");
}
// Add the main program code into the continuous loop() function
void loop()
{
}
根据Arduino的描述PROGMEM, you cannot access the data through pointers to it directly as with plain pointers. You need to use the proper macros/functions访问数据
在您的代码中,指针 table 本身位于 PROGMEM 中,因此,要提取各个指针,您应该执行如下操作:
const char** submenu = (const char**)pgm_read_word(&(mainmenu_table[1]));
const char* option = (const char*)pgm_read_word(&(submenu[i]));
if (option == option6) {
//...
此代码基于第一个 link 中的字符串 table 示例。
尝试构建菜单系统,但 运行 遇到一些指针问题 - 我没有太多经验。
我不明白为什么删除 while(1) 会使 mainmenu_table[1][i] == &option6 之间的比较失败,但出于某种原因确实如此。
我做错了什么?使用 visual studio 和 atmega328p。谢谢
原始代码的串行输出:
Serial begins
MeNu6
MeNu6
Starting compare loop
it worked
删除了 while(1) 的串行输出。
Serial begins
MeNu6
MeNu6
Starting compare loop
the end
原始代码(包含 while(1))
const char option1[] PROGMEM = "Menu1";
const char option2[] PROGMEM = "MEnu2";
const char option3[] PROGMEM = "MeNu3";
const char option4[] PROGMEM = "Menu4";
const char option5[] PROGMEM = "MEnu5";
const char option6[] PROGMEM = "MeNu6";
const char option7[] PROGMEM = "menu7";
const char* const submenu1_table[] PROGMEM = { option1, option2, option3 }; // array of pointers to chars stored in flash
const char* const submenu2_table[] PROGMEM = { option4, option5, option6, option7 };
const char** const mainmenu_table[] PROGMEM = { submenu1_table, submenu2_table }; //array of pointers to pointers to chars in flash
// The setup() function runs once each time the micro-controller starts
void setup()
{
Serial.begin(9600);
delay(100);
Serial.println("Serial begins");
Serial.println((const __FlashStringHelper*)(mainmenu_table[1][2])); // prints "Menu6" as expected
Serial.println((const __FlashStringHelper*)option6); // also prints "Menu6"
Serial.println("Starting compare loop");
for (int i = 0; i < 4; i++) {
if ( mainmenu_table[1][i] == &option6 ) { //
Serial.println("it worked");
while (1); // COMMENTING THIS OUT MEANS DOESN'T COMPARE SUCCESSFULLY.
}
}
Serial.println("the end");
}
// Add the main program code into the continuous loop() function
void loop()
{
}
根据Arduino的描述PROGMEM, you cannot access the data through pointers to it directly as with plain pointers. You need to use the proper macros/functions访问数据
在您的代码中,指针 table 本身位于 PROGMEM 中,因此,要提取各个指针,您应该执行如下操作:
const char** submenu = (const char**)pgm_read_word(&(mainmenu_table[1]));
const char* option = (const char*)pgm_read_word(&(submenu[i]));
if (option == option6) {
//...
此代码基于第一个 link 中的字符串 table 示例。