简体   繁体   English

无法访问数组结构的所有元素

[英]Unable to access all elements of Array Structure

I am coding a GUI for my menu.我正在为我的菜单编写一个 GUI。 The problem is this that when I access the drawtext function only the first element of my sub_menu char array is getting displayed when I access the function using问题是,当我访问 drawtext 函数时,只有当我使用访问该函数时,才显示 sub_menu 字符数组的第一个元素

        drawText(38,195,*a->sub_Menu[1],0);
        drawText(38,240, a->sub_Menu[2],0);
        drawText(38,285, a->sub_Menu[3],0);
        drawText(38,330, a->sub_Menu[4],0);

and rest of the boxes show up blank.其余的框显示为空白。 And when I try to access the drawtext funcion using当我尝试使用

        drawText(38,195,*a->sub_Menu[1],0);
        drawText(38,240,*a->sub_Menu[2],0);
        drawText(38,285,*a->sub_Menu[3],0);
        drawText(38,330,*a->sub_Menu[4],0);

the program compiles and runs but as soon as I click on Settings button of my menu the program crashes saying myprogram.exe has stopped working.程序编译并运行,但只要我点击菜单的设置按钮,程序就会崩溃,说 myprogram.exe 已停止工作。 I don't know what the problem is as I am new to coding.我不知道问题是什么,因为我是编码新手。

    typedef struct {
    short startXPos;
    short startYPos;
    short height;
    short width;
    unsigned int c;
    char *sub_Menu[5][18];
} menu, *ptr_Menu;

ptr_Menu a;
char sub_Menu1[5][18] = {"Big Font", "5 channel", "7 channel", "12 channel", "Alarm"};
menu touch_menu[10] = {30, 365, 45, 100, 5, &sub_Menu1};

void drawMenu(short b)
{
    int k = 0;
    if (b == 0) {
        a = &touch_menu[0];
        for (k=0; k<a->c; k++) {
            setColor(GREY);
            drawRectangle(a->startXPos, a->startYPos - (k+1)*a->height,a->width,a->height);
        }
        setColor(CYAN);
        drawText(38,150,*a->sub_Menu[0],0);
        drawText(38,195,*a->sub_Menu[1],0);
        drawText(38,240,*a->sub_Menu[2],0);
        drawText(38,285,*a->sub_Menu[3],0);
        drawText(38,330,*a->sub_Menu[4],0);
    }
}

Your problem is that you think你的问题是你认为

char *sub_Menu[5][18];

is a pointer to a 5x18 character array.是一个指向 5x18 字符数组的指针。 But in reality it's a 5x18 array of char pointers.但实际上它是一个 5x18 的字符指针数组。

Change your struct type like this:像这样改变你的结构类型:

char *sub_Menu[5];

And the initialization:和初始化:

menu touch_menu[10] = {
    { 30, 365, 45, 100, 5, { "Big Font", "5 channel", "7 channel", "12 channel", "Alarm" } },
    // remaining 9 menu data comes here
};

And drawing the text.并绘制文字。 You'd be better off with a loop.你最好有一个循环。 DRY (Don't Repeat Yourself). DRY(不要重复自己)。

drawText(38, 240, a->sub_Menu[2], 0);

The strings are stored in the constant area of your program, they contain a terminating zero, so you don't have to worry about their length (18).字符串存储在程序的常量区域中,它们包含一个终止零,因此您不必担心它们的长度 (18)。 All you need is an array of 5 character pointers in your struct.您所需要的只是结构中包含 5 个字符指针的数组。

As to the initializer, it needs 3 levels of nested {} symbols:至于初始化器,它需要 3 层嵌套的{}符号:

  • 1st level because touch_menu is an array第一级,因为 touch_menu 是一个数组
  • 2nd level because it contains structs第二级,因为它包含结构
  • 3rd level because each struct contains an array第 3 级,因为每个结构都包含一个数组

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

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