我一直在试图弄清楚我的这段代码有什么问题。所以到处都是我的代码,我想我已经找到了罪魁祸首。这是这块代码在这里造成它我该如何解决这种分段故障?
if (strcmp(input, "load 1") != 0 || strcmp(input, "load 2") != 0 || strcmp(input, "quit") != 0)
{
Player player;
Position position;
Direction direction;
char* tok;
char* firstInput=0;
char* secondInput=0;
char* thirdInput=0;
char* fourthInput=0;
int x;
int y;
char str[10];
char str2[10];
tok = strtok(input, " ");
strcpy(firstInput, tok);
tok = strtok(NULL, ", ");
strcpy(secondInput, tok);
tok = strtok(NULL, ", ");
strcpy(thirdInput, tok);
tok = strtok(NULL, "");
strcpy(fourthInput, tok);
strcpy(str, secondInput);
x = atoi(str);
strcpy(str2, thirdInput);
y = atoi(str2);
if(strcmp(firstInput, "init") == 0)
{
if(x >= 0 && x <= 9)
{
if(y >= 0 && y <= 9)
{
if (strcmp(fourthInput,"north") == 0 || (strcmp(fourthInput,"south")) || (strcmp(fourthInput,"west") == 0) || (strcmp(fourthInput,"east") == 0))
{
position.x = x;
position.y = y;
if(strcmp(fourthInput,"north") == 0)
{
direction = NORTH;
}
if(strcmp(fourthInput,"south") == 0)
{
direction = SOUTH;
}
if(strcmp(fourthInput,"west") == 0)
{
direction = WEST;
}
if(strcmp(fourthInput,"east") == 0)
{
direction = EAST;
}
initialisePlayer(&player, &position, direction);
placePlayer(board, position);
displayDirection(direction);
}
}
}
}
}
从我所知道的分段故障意味着一个内存问题。我已确保有足够的空间容纳一切。实际上发生了什么?
可能的重复[如何调试段错误?](http://stackoverflow.com/questions/29324796/how-to-debug-segmentation-fault) – OrangeDog
嗯,你还没有分配任何内存。 'firstInput'仍然是0. –
哪条线段错误?什么是回溯? – fluter