Multi-dimensional array error

I'm having an error when working with a multi-dimensional array for my Tic Tac Toe game.

The problem is in the constructor and the destructor of the class, but they are the same so I will just post the constructor -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define BOARD_WIDTH 3  //dimensions
#define BOARD_HEIGHT 3
 
char board[BOARD_HEIGHT][BOARD_WIDTH]; //game board

TicTacToe::TicTacToe()
{
    for (int i = 0; i < BOARD_HEIGHT; i++)
    {
        for (int o = 0; o < BOARD_WIDTH; i++)
        {
            board[i][o] = ' ';
        }
    }
}


I have tried debugging and as soon as I start the debugger (Code::Blocks), it throws error
Program received signal SIGSEGV, Segmentation fault. Do you want to view the backtrace?
.
Backtrace leads to this line -
board[i][o] = ' ';

I don't think it's an error with the ' ' since if I try something like - board[2][0] = ' '; it compiles fine.

Can anyone help?
I'd have to brush up on my knowledge (or lack thereof) of multidimensional arrays, as I never use them like that (I prefer to use them as arrays of pointers instead of arrays of arrays) but I'm going to suggest it's something to do with the fact that board is an array of BOARD_WIDTH arrays of BOARD_HEIGHT. I'm also going to suggest that you're probably "going off the end" of the arrays.
What you have posted there is without error. A debugger is not always accurate when reporting the line associated with errors. What did your program do just before calling the ctor?
Last edited on
Yes, check the lines before it.
but I'm going to suggest it's something to do with the fact that board is an array of BOARD_WIDTH arrays of BOARD_HEIGHT

What does that mean?

I'm also going to suggest that you're probably "going off the end" of the arrays.

Does that mean i'm going over the limits (i.e. board[3][3])? I'm pretty sure, if that's what you mean, it's not happening.

EDIT : Oh crap lol... just as I post 2 people post before me :\
Last edited on
Probably you meant

for (int o = 0; o < BOARD_WIDTH; o++) // here you have written i++ instead of o++

Hope this helps !
kevinchkin, how come I get the feeling you're right? :\

Thanks everyone for helping.... feel so embarrassed that it's such a minor mistake in the code...
Marking topic as solved.
Last edited on
Topic archived. No new replies allowed.