簡體   English   中英

指向const數組的指針

[英]pointer to const array of ints

我已經在嵌入式系統中創建了一個菜單結構,如下所示:

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)
{
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM