简体   繁体   English

指向const数组的指针

[英]pointer to const array of ints

I've created a menu structure in an embedded system as follows: 我已经在嵌入式系统中创建了一个菜单结构,如下所示:

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
};

Which is instantiated thus for early prototyping: 实例化为早期原型:

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
};

with a view to initialising function/menu pointers and the like later 为了稍后初始化功能/菜单指针等

It's compiling ok with GCC but when the code is running (there is a non-dynamic menu system up and running already) the IDE (MPLABX) is struggling to view the contents MainMenu and when I try and run the function DisplayMenuItems (as below) I can't step through it 使用GCC可以正常编译,但是当代码运行时(已经有一个非动态菜单系统在运行),IDE(MPLABX)难以查看内容MainMenu,而当我尝试运行函数DisplayMenuItems时(如下所示)我无法通过它

Can anyone spot anything wrong with the way I've defined the struct and instantiated it? 有人可以发现我定义结构和实例化结构的方式有问题吗?

I plan to use the following function to display items using the struct: 我计划使用以下功能来显示使用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 might be letting me get away with something I'm not supposed to here so any tips appreciated! GCC可能会让我摆脱本不该在这里遇到的问题,因此任何提示都值得赞赏!

Many thanks 非常感谢

The problem is probably that you are modifying the pointer in the loop. 问题可能出在修改循环中的指针。 That means that if the DisplayMenuItems function is called a second time, the pointer will no longer point to the MainMenuTextStrings array, but beyond it and accessing that data leads to undefined behavior . 这意味着,如果第二次调用DisplayMenuItems函数,则指针将不再指向MainMenuTextStrings数组,而是指向该数组之外,访问该数据将导致未定义的行为

Instead of modifying the pointer, why not simply use array indexing syntax? 为什么不简单地使用数组索引语法而不是修改指针?

Like 喜欢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM