尝试打印菜单名称,使用结构,控制台被绞死,虽然结果打印?

时间:2013-08-21 10:20:22

标签: c

我正在尝试在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;
}

2 个答案:

答案 0 :(得分: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的数组范围。

答案 1 :(得分: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