无法访问Array Structure的所有元素 [英] Unable to access all elements of Array Structure

查看:81
本文介绍了无法访问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);
    }
}

推荐答案

你在这里采用了一些笨拙的想法。二维阵列是有问题的。我无法编译你的代码,因为sub_Menu被声明为一个大小为5的数组,大小为18个指向char的数组,你不能获取sub_Menu1的地址并将其分配给它。



但是

You have employed some unwieldy ideas here. The two dimensional array is problematic. I can't compile your code because sub_Menu is declared as an array of size 5 of arrays of size 18 pointers to char and you can't take the address of sub_Menu1 and assign that to it.

However
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的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆