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.
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. But in reality it's a 5x18 array of char pointers.
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).
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). All you need is an array of 5 character pointers in your struct.
As to the initializer, it needs 3 levels of nested {}
symbols:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.