0

我正在尝试在 TFT 上的预定义位置打印菜单名称,我可以在 TFT 上打印这些名称。但是,当我使用带有控制台输出的 Dev-C++ 编译器尝试此代码时,控制台窗口上的结果正在打印但收到消息 program.exe 停止工作?我很困惑,请帮助我。!

#include <stdio.h>
#include <Windows.h>
//defind a struct with icon details
typedef struct
{
    unsigned int icon_num;
    unsigned char* first_name;
    unsigned char* last_name;
    unsigned int name_pos1;
    unsigned int name_pos2;
}xIcon_Info;

// A structure variable for xIcon_info
xIcon_Info MainIcons_Info[]=
{
    {0,     "FName_1","LName_1",            11,15},
    {1,     "FName_2","LName_2",            80,76},
    {2,     "FName_3","LName_3",            148,142},
    {3,     "FName_4","LName_4",            208,202},
    {4,     "FName_5","LName_5",            258,255},

    {5,     "FName_6","LName_6",            10,10},
    {6,     "FName_7","LName_7",            93,78},
    {7,     "FName_8","LName_8",            138,131},
    {8,     "FName_9","LName_9",            202,202},
    {9,     "FName_10","LName_10",          255,258},

    {10,    "FName_11","LName_11",          21,10},
    {11,    "FName_12","LName_12",          78,72},
    {12,    "FName_13","LName_13",          135,132},
    {13,    "FName_14","LName_14",          198,195},
    {14,    "FName_15","LName_15",          255,255}

};

//defined a structure with screen details        
typedef struct
{
    unsigned int no_of_icons;
    unsigned int level;
    unsigned int current_icon;
    xIcon_Info* icons_info;
    unsigned int name_pos1;
    unsigned char screen_image_base;
}xScreen_Info;


//defind a structer variable with init for xScreen_Info        
xScreen_Info MainScreen = 
{
    15,
    1,
    0,
    &MainIcons_Info[0],
    //(unsigned char *)0xa0196001,  // Starting imgaes
};

xScreen_Info* xpCurrent_Screen = &MainScreen;

int main(void)
{
    int i = 0;

    for(i=0; i<=(xpCurrent_Screen->no_of_icons); i++)
    {
    printf("First name: %s\t",(((xpCurrent_Screen->icons_info)+i)->first_name));
    printf("Last Name: %s\n",(((xpCurrent_Screen->icons_info)+i)->last_name));
    }
    system("PAUSE");
    return 0;
}
4

2 回答 2

2

问题在于您的循环条件:

for(i=0; i<=(xpCurrent_Screen->no_of_icons); i++)

您循环 16 次,而不是 15 次,从而访问xpCurrent_Screen->icons_info导致崩溃的越界。

改成

for(i=0; i < xpCurrent_Screen->no_of_icons; i++)

反而。


如果您尝试在调试器中运行,它将在崩溃行(其中一个printf调用)处停止,如果您随后打印变量i,您会看到它15超出了大小为 15 的数组的范围。

于 2013-08-21T10:24:13.383 回答
1

一个错误:该for行应该是:

for(i=0; i<(xpCurrent_Screen->no_of_icons); i++)

此外,(((xpCurrent_Screen->icons_info)+i)->first_name)看起来过于复杂和有点傻。为什么不直接使用xpCurrent_Screen->icons_info[i].first_name

于 2013-08-21T10:24:54.970 回答