Okay, I got it.
You're defintely using a C99 compliant compiler, that allows variable length arrays.
Relevant extracts from your code:
#define MAX 9
int board[MAX][MAX]; // <- board is an int[9][9]
int d; // <- d is a global variable
bool search(
int value,
int values[MAX][d], // <- tells compiler to handle values as int[9][d]
int n);
// from within init()
for(int row =0; row<d; row++)
for(int col=0; col<d; col++)
board[row][col]=value--; // <- board inited as an int[9][9]
A fixed size array is a big lump of contiguous memory, with rows stored one after the other.
Example:
int a[2][3]
is stored as 6 ints corresponding to:
a[0][0]
a[0][1]
a[0][2]
a[1][0]
a[1][1]
a[1][2]
here board
memory layout is 9x9 :
000000000
111111111
222222222
333333333
444444444
555555555
666666666
777777777
888888888
or in linear memory:
000000000111111111222222222333333333444444444555555555666666666777777777888888888
Now if d is 4 as your screenshot shows, the search function will think its layout is :
0000
1111
2222
3333
4444
5555
6666
7777
8888
or in linear memory:
000011112222333344445555666677778888
Your init function uses the 9x9 layout, so it puts the values like this:
15 14 13 12 x x x x x
11 10 9 8 x x x x x etc.
but your search function reads them like :
15 14 13 12
x x x x
x 11 10 9
8 x x x
etc.
Basically, you declared a structure for your array in the prototype of search()
that is inconsistent with its declaration.
You recklessly violated one of the C little known golden rules :
always keep function parameters and array declarations consistent.
and C punished you with a cryptic bug.
Read this little essay of mine for more details.