指向int的const数组的指针

mr_Alex_Nok_

我已经在嵌入式系统中创建了一个菜单结构,如下所示:

struct DisplayMenu
{
    short int MenuTitle;                                        // Menu title text offset - the title used in the calling menu
    char NoOfSubmenuItems;                                      // number of submenuItems in this menu ? static!
    char PositionInLastMenu;                                    // position in previous menu - helps restore previous menu screen
    struct DisplayMenu *PreviousMenu;                           // pointer to previous menu - need to be const?
    struct DisplayMenu *NextMenu[MAX_NO_OF_SUBMENU_ITEMS];      // array of pointers to submenus structs - Null if no submenu...function call instead
    void (*MenuFunctionsArray[MAX_NO_OF_SUBMENU_ITEMS])(void);  // array of function pointers - called if no submenu. Check if null
    const short int *SubMenuText;                               // pointer array of text offsets for submenu text - no set length variable length array allowed at end of struct in GCC at least
};

实例化为早期原型:

const short int MainMenuTextStrings[]= { tSTATUS, tSETUP, tStartOfTextString, tANALYSER, tUNITS, tEndOfTextString, tUNITS, tALARM, tSCREEN, tREPORTS, tSERVICE, tStartOfTextString, tMANUAL, tAIR, tZERO, tEndOfTextString, tStartOfTextString, tMANUAL, tEndOfTextString, NULL };     // needs a new text entry

// main menu struct
struct DisplayMenu MainMenu =
{
    tMENU,                                                                                              // Menu title
    0,                                              // number of submenus
    0,                                                                                                  // no previous menu 
    NULL,                                                                                               // no previous menu to point to
    { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },                                     // to be set up later
    { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },                                     // function pointers
    MainMenuTextStrings
};

以期稍后初始化功能/菜单指针等

使用GCC可以正常编译,但是当代码运行时(已经在运行非动态菜单系统),IDE(MPLABX)难以查看MainMenu的内容,而当我尝试运行DisplayMenuItems函数时(如下所示)我无法逐步解决

有人可以发现我定义结构并实例化该结构的方式有问题吗?

我计划使用以下功能来显示使用struct的项目:

void DisplayMenuItems(struct DisplayMenu* Menu)
{

    while(*(Menu->SubMenuText) != NULL)
    {
        if(*(Menu->SubMenuText) == tStartOfTextString)       //if a few text strings need to be concatenated on one line
        {
            while(*(Menu->SubMenuText++) != tEndOfTextString)  //keep printing until you find the EndofTextString ident
                WriteStringToDisplay((char*) &pText[Config.ucLanguage][*(Menu->SubMenuText)], SmallFont);
        }
        else /*if(*(Menu->SubMenuText) != NULL) */
        {
            WriteStringToDisplay((char*) &pText[Config.ucLanguage][*(Menu->SubMenuText)++], SmallFont);
        }
        DisplayCarriageReturn(2,SmallFont);         // try and remove this eventually? Just increment Lcd.ucLine instead
    }
}

GCC可能会让我摆脱本不该在这里碰到的东西,所以任何提示都值得赞赏!

非常感谢

一些程序员哥们

问题可能出在修改循环中的指针。这意味着,如果DisplayMenuItems第二次调用函数,则指针将不再指向MainMenuTextStrings数组,而是指向数组之外,并且访问该数据将导致未定义的行为

为什么不简单地使用数组索引语法而不是修改指针?

喜欢

for (unsigned int i = 0; Menu->SubMenuText[i] != NULL; ++i)
{
    ...
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C ++指向const指针的const数组的指针

来自分类Dev

指向C中的int指针数组的指针

来自分类Dev

指向C ++中const int的指针

来自分类Dev

C ++制作指向const对象的指针数组

来自分类Dev

“指向const int的指针”是否与“ const int *”相同

来自分类Dev

C ++ int指向数组的指针指向数组内存泄漏

来自分类Dev

使用智能指针指向const int的指针

来自分类Dev

管理指向int数组的指针的问题

来自分类Dev

指向int和char的指针(数组)

来自分类Dev

C const 指向 const struct 数组的指针作为函数参数

来自分类Dev

了解指向常数的指针指向整数常数的指针(const int * const *变量)

来自分类Dev

指向const指针(或指向const的指针)

来自分类Dev

c-将指向const int的指针转换为仅指向int的指针(并对其进行修改)

来自分类Dev

指向指针数组的指针

来自分类Dev

在递归调用中传递指向const int的指针

来自分类Dev

指向int的指针的c ++指针

来自分类Dev

ctypes指向c_int与c_ints数组的指针

来自分类Dev

指向int数组的指针提供与取消引用时相同的地址

来自分类Dev

如何检查指针是否指向数组或单个int或char

来自分类Dev

在Swift中与指向Int数组的C指针互操作

来自分类Dev

指向int类型数组的指针的大小是多少?

来自分类Dev

指向数组中指针的指针

来自分类Dev

指向函数指针数组的指针

来自分类Dev

指向指针向量的指针数组

来自分类Dev

C - 指向指针数组的指针

来自分类Dev

指向数组的指针错误

来自分类Dev

指针数组(指向结构)

来自分类Dev

指向数组的指针

来自分类Dev

删除指向数组的指针