【发布时间】:2016-04-25 18:43:59
【问题描述】:
我有时会遇到上述错误,我知道这是由于以下代码造成的,但不知道可能是什么原因,因为我对 C 语言还是很陌生。
struct Point playerPosition;
int counter = 0;
do {
playerPosition.x = rand() % mapLength;
playerPosition.y = rand() % mapHeight;
counter++;
printf("The current tile that is being tried is: %c\n", map[playerPosition.x][playerPosition.y]);
} while((counter < (mapLength*mapHeight)) && ((map[playerPosition.x][playerPosition.y] == '#') || (map[playerPosition.x][playerPosition.y] == 'P')));
printf("A tile was found at %d %d, with the tile being %c\n", playerPosition.x, playerPosition.y, map[playerPosition.x][playerPosition.y]);
【问题讨论】:
-
maplength/mapheight定义在哪里,它们与map有什么关系? -
还有
struct Point(虽然我们可以猜到它的声明)参见:How to create a Minimal, Complete, and Verifiable example。” -
我在您的代码 sn-p 中没有看到定义
map的任何地方。这肯定会导致段错误。 -
数据类型/声明至少和代码一样重要。你应该包括他们。
标签: c struct segmentation-fault