本文介绍了无法访问Array Structure的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的菜单编写GUI。问题是,当我访问drawtext函数时,只有当我使用



drawText(38,195,*)访问函数时,才会显示我的sub_menu char数组的第一个元素。 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);

和其他方框显示为空白。当我尝试使用



drawText(38,195,* a-> sub_Menu [1],0)访问drawtext功能时;

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

drawText(38,285,* a-> sub_Menu [3],0);

drawText(38,330,* a-> sub_Menu [4],0);

程序编译并运行但是当我单击菜单上的设置按钮时,程序崩溃说myprogram.exe已停止工作。我不知道问题是什么,因为我不熟悉编码。请帮助我。

I am coding a GUI for my menu. 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(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. I don't know what the problem is as I am new to coding. Kindly Help me out.

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

推荐答案

menu touch_menu[10] = {30, 365, 45, 100, 5, &sub_Menu1};



仅初始化第一个成员touch_menu。



看看这个可能会解决你的问题:




only initialises the first member of touch_menu.

Look at this and it may solve your problem:

#include <iostream>
using namespace std;

typedef struct {
    short startXPos;
    short startYPos;
    short height;
    short width;
    unsigned int c;
    char (*sub_Menu)[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];

int main()
{
	for(int i=0; i <10; i++)
	{
		touch_menu[i].c = i;
		touch_menu[i].startXPos = 30;
		touch_menu[i].startXPos = 365;
		touch_menu[i].sub_Menu = sub_Menu1;
	}

    a = &touch_menu[0];

	for(int i=0; i <10; i++)
		cout << (a + i)->c << "   " << (a+i)->sub_Menu[0] << endl;

	return 0;
}


这篇关于无法访问Array Structure的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 22:13