# Segmentation fault in pset3 Game of Fifteen

I'm a little lost. I've been trying to debug this segmentation fault for a few days now and still can't figure out what to do. I narrowed the problem down to a particular line of code thanks to a combination of debug50 and a printf command.

The code in question:

``````bool move(int tile)
{
if (tile > d * d - 1 || tile < 1)
{
return false;
}

int r = 0;
int c = 0;

for (int r = 0; i < d; i++)
{
for (int c = 0; j < d; i++)
{
if (tile == board[i][j])
{
int blank = 0;
if (((r == (i - 1)) && (c == j)) || ((r == (i + 1)) && (c == j)) ||
((r == i) && (c ==(j - 1))) || ((r == i) && (c == (j + 1))))
{
board[r][c] = tile;
board[i][j] = blank;
r = i;
c = j;

return true;
}
}
}
}
return false;
}
``````

The segmentation fault appears to trigger when I call "if (tile == board[i][j])". For some reason or other, when I was using the debugger, it seems to think that the variable of "i" is a value of 3864 while the variable of "tile" is a value of 3. All other variables involved are at a value of 0.

I'm stumped by this. Can anyone point me in the right direction?

• You don't seem to declare `i` and `j` at all, but `r` and `c` twice? Commented Nov 10, 2017 at 18:59