很奇怪的“段错误”

软件和网站开发以及相关技术探讨
回复
头像
Rothsdad
帖子: 396
注册时间: 2009-04-18 11:41

很奇怪的“段错误”

#1

帖子 Rothsdad » 2009-05-13 18:55

#include <stdio.h>

#define XMAX 3
#define YMAX 3

int map[XMAX][YMAX] = {{0, 0, 0}, {0, 1, 0}, {0, 1, 2}};
struct Status
{
int x, y;
}status[999];
int path;

int spy(int x, int y); //检测四周
void move(int *x, int *y); //移动一步

int main()
{
status[0].x = 0;
status[0].y = 0;
path = 0;

while (1)
{
if (spy(status[path].x, status[path].y) == 0)
path--;
else
{
move(&(status[path].x), (&status[path].y));
}
if((spy(status[path].x, status[path].y) == 0) && (path == 0))
{
printf("no result!\n");
return 0;
}
if (map[status[path].x][status[path].y] == 2)
{
printf("OK!");
}
}
}

int spy(int x, int y)
{
if ((y > 1) && (map[x, (y - 1)] == 0))
return 1;
if ((x > 1) && (map[(x - 1), y] == 0))
return 2;
if ((y < (YMAX - 1)) && (map[x, (y + 1)] == 0))
return 3;
if ((x < (XMAX - 1)) && (map[(x + 1), y] == 0))
return 1;
return 0;
}

void move(int *x, int *y)
{
if (spy(*x, *y) == 1)
(*y)--;
else if (spy(*x, *y) == 2)
(*x)--;
else if (spy(*x, *y) == 3)
(*y)++;
else if (spy(*x, *y) == 4)
(*x)++;
}

编译后执行出现段错误,为什么呢?
重温经典,回归MUD
viewtopic.php?f=34&t=235277

我的博客,一些心得,欢迎分享哦
http://hi.baidu.com/rothsdad/home
头像
libralibra
帖子: 401
注册时间: 2008-02-23 17:31
联系:

Re: 很奇怪的“段错误”

#2

帖子 libralibra » 2009-05-13 20:16

看不明白

spy永远return 0吗?
My Blog: matlab, ubuntu, python
http://goo.gl/GDIO
头像
Rothsdad
帖子: 396
注册时间: 2009-04-18 11:41

Re: 很奇怪的“段错误”

#3

帖子 Rothsdad » 2009-05-13 21:09

不是的,由于粘贴到论坛上时,tab缩进没有了,所以看上去很别扭的。
if后只跟一条语句,所以并不是永远return 0;

我又调试了一下,貌似是数组越界~
重温经典,回归MUD
viewtopic.php?f=34&t=235277

我的博客,一些心得,欢迎分享哦
http://hi.baidu.com/rothsdad/home
回复