C 打印二维数组时出现致命错误

C 打印二维数组时出现致命错误,c,arrays,gameboy,C,Arrays,Gameboy,我遇到了一个小代码块的问题: for(x = 0; x < 8; x++){ for(y = 0; y < 8; y++){ printf(" %d", occupy[x][y]); } printf(" \n"); } (x=0;x

我遇到了一个小代码块的问题:

for(x = 0; x < 8; x++){
for(y = 0; y < 8; y++){
printf(" %d", occupy[x][y]);
}
printf(" \n");
}
(x=0;x<8;x++)的
{
对于(y=0;y<8;y++){
printf(“%d”,占据[x][y]);
}
printf(“\n”);
}
我正在为Gameboy制作一个随机生成的迷宫游戏,我正在使用2d数组来知道每个屏幕在迷宫中的位置。为了进行测试,我尝试打印该数组,以便在继续之前查看其生成是否正确。当我试图用那一小段代码进行编译时,我在代码的最上面一行得到一个错误,然后在文件中说致命的编译器内部错误等等。在我不知道的代码中是否有一个很大的“不”字

完整代码:

#include <gb\gb.h>
#include <stdio.h>
#include <rand.h>


#define UP 0x01U
#define RIGHT 0x02U
#define DOWN 0x04U
#define LEFT 0x08U

int occupy[8][8]= {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};


void generate(){
int temp;
int x;
int y;
UBYTE restrict;
UBYTE direction;
for(x = 0; x < 8; x++){
for(y = 0; y < 8; y++){
occupy[x][y] = 0;
}
}
/* ========Occupy=Cleared========== */
restrict = 0x00U;
x = rand() & 7;
y = rand() & 7;

if(x == 6 || x == 7){ restrict += RIGHT;}
if(x == 0 || x == 1){ restrict += LEFT;}
if(y == 0 || y == 1){ restrict += UP;}
if(y == 6 || y == 7){ restrict += DOWN;}

/* in the rest of generation wrap this block in if(restrict != 0x0FU){ */

do{
temp = rand() & 3;
if(temp == 0){ direction = UP;}
if(temp == 1){ direction = RIGHT;}
if(temp == 2){ direction = DOWN;}
if(temp == 3){ direction = LEFT;}
}while(restrict & direction);


occupy[x][y] = 5;
if(direction == UP){ occupy[x][y-1] = 1;}
if(direction == RIGHT){ occupy[x+1][y] = 2;}
if(direction == DOWN){ occupy[x][y+1] = 3;}
if(direction == LEFT){ occupy[x-1][y] = 4;}

for(x = 0; x < 8; x++){
for(y = 0; y < 8; y++){
printf(" %d", occupy[x][y]);
}
printf(" \n");
}

}


void main(){
generate();

}
#包括
#包括
#包括
#定义0x01U
#定义右0x02U
#定义向下0x04U
#定义左0x08U
int占领[8][8]={
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
void生成(){
内部温度;
int x;
int-y;
UBYTE限制;
UBYTE方向;
对于(x=0;x<8;x++){
对于(y=0;y<8;y++){
占用[x][y]=0;
}
}
/*==========占用=清除=================*/
限制=0x00U;
x=rand()&7;
y=rand()&7;
如果(x==6 | | x==7){restrict+=RIGHT;}
如果(x==0 | | x==1){restrict+=LEFT;}
如果(y==0 | | y==1){restrict+=UP;}
如果(y==6 | | y==7){restrict+=DOWN;}
/*在生成的其余部分中,使用if(restrict!=0x0FU){*/
做{
temp=rand()&3;
如果(temp==0){direction=UP;}
如果(temp==1){direction=RIGHT;}
如果(temp==2){direction=DOWN;}
如果(temp==3){direction=LEFT;}
}while(限制和指导);
占据[x][y]=5;
如果(方向==向上){ocking[x][y-1]=1;}
如果(方向==右){占据[x+1][y]=2;}
如果(方向==向下){占据[x][y+1]=3;}
如果(方向==左){占据[x-1][y]=4;}
对于(x=0;x<8;x++){
对于(y=0;y<8;y++){
printf(“%d”,占据[x][y]);
}
printf(“\n”);
}
}
void main(){
生成();
}
以下编译
#包括
#include//包含rand和srand函数原型
#包括
//#包括“gb\gb.h”
作废生成(作废);
#定义0x01U
#定义右0x02U
#定义向下0x04U
#定义左0x08U
typedef无符号字符;
int[8][8]=
{
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
int main()
{
生成();
返回(0);
}
void生成()
{
内部温度;
int x;
int-y;
UBYTE阻滞;
UBYTE方向;
对于(x=0;x<8;x++)
{
对于(y=0;y<8;y++)
{
占用[x][y]=0;
}
}
/*==========占用=清除=================*/
块=0x00U;
srand(time(NULL));//初始化rand()
x=rand()&8;
y=rand()&8;
如果(x==6 | | x==7){block+=RIGHT;}
如果(x==0 | | x==1){block+=LEFT;}
如果(y==0 | | y==1){block+=UP;}
如果(y==6 | | y==7){block+=DOWN;}
/*在生成的其余部分中,将此块用if(block!=0x0FU){*/
//注:
//在下面的while循环中,如果方向不相同,则为块
//然后循环将立即退出
//可能不是你想做的。
做{
temp=rand()&4;
如果(temp==0){direction=UP;}
如果(temp==1){direction=RIGHT;}
如果(temp==2){direction=DOWN;}
如果(temp==3){direction=LEFT;}
}while(阻塞和方向);
//注:
//在下面的代码中,
//“x”和/或“y”可以是0或7
//在任何一种情况下,以下一个或多个语句
//可能/将导致访问超出范围
//占用[][]数组的
占据[x][y]=5;
如果(方向==向上){ocking[x][y-1]=1;}
如果(方向==右){占据[x+1][y]=2;}
如果(方向==向下){占据[x][y+1]=3;}
如果(方向==左){占据[x-1][y]=4;}
对于(x=0;x<8;x++)
{
对于(y=0;y<8;y++)
{
printf(“%d”,占据[x][y]);
}
printf(“\n”);
}
}

这是在我的GBDK fine版本下编译的,尽管我不确定为什么我所做的更改会修复它

所作的修改:

  • 占领
    现在是一个
    无符号整数
    数组,而不仅仅是
    整数
    (没有此更改
    sdcc
    崩溃)
  • 循环内的
    printf
    调用已从
    %d”
    更改为
    %u”
    (因为该值未签名)
  • 添加了一个显示占用地址的
    printf
    。如果我将其删除或更改以执行其他操作,
    sdcc
    将崩溃。这很愚蠢,但似乎是必需的。我正在寻找不需要无意义的
    printf
    的替代方案

    • 请注意,可以改为使用以下内容,这将导致不打印任何内容(例如,它尝试写入地址
      $0000
      ,但这是只读的,并且失败)。它仍然很愚蠢,但不显示任何内容。不过,它可能会导致某些速度减慢

      sprintf(0,“,&ocking);
      
  • 我还改进了缩进

#包括
#包括
#包括
#定义0x01U
#定义右0x02U
#定义向下0x04U
#定义左0x08U
无符号整数占用[8][8]={
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
void生成(){
内部温度;
int x;
int-y;
UBYTE限制;
UBYTE方向;
对于(x=0;x<8;x++){
对于(y=0;y<8;y++){
占用[x][y]=0;
}
}
/*==========占用=清除=================*/
限制=0x00U;
x=rand()&7;
y=rand()&7;
如果(x==6 | | x==7){restrict+=RIGHT;}
如果(x==0 | | x==1){restrict+=LEFT;}
如果(y==0 | | y==1){restrict+=UP;}
如果(y==6 | | y==7){restrict+=DOWN;}
/*在t
the following compiles



#include <stdio.h>
#include <stdlib.h> // contains rand and srand function prototypes
#include <time.h>
//#include "gb\gb.h"

void generate( void );

#define UP 0x01U
#define RIGHT 0x02U
#define DOWN 0x04U
#define LEFT 0x08U
typedef unsigned char UBYTE;

int occupy[8][8] =
{
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0}
};

int main()
{
    generate();
    return(0);
}


void generate()
{
    int temp;
    int x;
    int y;
    UBYTE block;
    UBYTE direction;

    for(x = 0; x < 8; x++)
    {
        for(y = 0; y < 8; y++)
        {
            occupy[x][y] = 0;
        }
    }
    /* ========Occupy=Cleared========== */
    block = 0x00U;
    srand(time(NULL) ); // initialize rand()
    x = rand() & 8;
    y = rand() & 8;

    if(x == 6 || x == 7){ block += RIGHT;}
    if(x == 0 || x == 1){ block += LEFT;}
    if(y == 0 || y == 1){ block += UP;}
    if(y == 6 || y == 7){ block += DOWN;}

    /* in the rest of generation wrap this block in if(block != 0x0FU){ */

    // NOTE:
    // in the following while loop if direction is not the same a block
    // then the loop will immediately exit
    // probably not what you want to do.
    do{
        temp = rand() & 4;
        if(temp == 0){ direction = UP;}
        if(temp == 1){ direction = RIGHT;}
        if(temp == 2){ direction = DOWN;}
        if(temp == 3){ direction = LEFT;}
    } while(block & direction);


    // NOTE:
    // in the following code,
    // 'x' and/or 'y' could be 0 or 7
    // in either case, one or more of the following statements
    // can/will cause accessing outside of the bounds
    // of the occupy[][] array
    occupy[x][y] = 5;
    if(direction == UP)   { occupy[x][y-1] = 1;}
    if(direction == RIGHT){ occupy[x+1][y] = 2;}
    if(direction == DOWN) { occupy[x][y+1] = 3;}
    if(direction == LEFT) { occupy[x-1][y] = 4;}

    for(x = 0; x < 8; x++)
    {
        for(y = 0; y < 8; y++)
        {
            printf(" %d", occupy[x][y]);
        }
        printf(" \n");
    }
}