-2

I'm having this:

char board_game [3][3] = {0}; // The Board Game

And after this I'm doing that:

scanf("%d%d", &row, &col); // Get The Input And Put It In Row And Column
printf("%d",board_game[row][col]);

There is some output that I don't understand and what does that line mean board_game[row][col]?

|    input    | output
| row  | col  |
|______|______|________
|   0  |   0  |   0
|------|------|--------
|   0  |   1  |   0
|------|------|--------
|   0  |   2  |   0
|------|------|--------
|   0  |   3  |   0
|------|------|--------
|   1  |   0  |   0
|------|------|--------
|   1  |   1  |   0
|------|------|--------
|   1  |   2  |   0
|------|------|--------
|   1  |   3  |   0
|------|------|--------
|   2  |   0  |   0
|------|------|--------
|   2  |   1  |   0
|------|------|--------
|   2  |   2  |   0
|------|------|--------
|   2  |   3  |   1     **WHY 1?**
|------|------|--------
|   3  |   0  |   1     **WHY 1?**
|------|------|--------
|   3  |   1  |   0
|------|------|--------
|   3  |   2  |   0
|------|------|--------
|   3  |   3  |   0
|------|------|--------

Can you please explain to me what is going on?

2 Answers2

0

You are trying to access a memory location that doesnot belong to your array. When you define an array of size 3 the index are 0,1,2 . The values board game[0][3],board game[1][3],board game[2][3],board game[3][0],board game[3][1], board game[3][2],board game[3][3] are out of bound

0

It's Out Of Bound and undefined (offsets: 3, 3| 3, 0) when the indexes only can be between 0..2

And you declared 0 at the start to all the array values

board_game[row][col] Means the value of the array in that offsets

Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36