Two Dim Array Segfaulting

I am trying to initialise and print a 10x10 array to the console. This code works, but I get a segfault at the end of it and I don't know why:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int main()
{
    int grid[9][9];
    initGrid(grid);
    printBoard(grid);
    return 0;
}

void initGrid(int grid[][9])
{
    for(int col=0; col<10; col++) //Outer column loop
    {
        for(int row=0; row<10; row++) //Inner row loop
        {
            grid[col][row]=0;
        }
    }
}

void printBoard(int grid[][9])
{
    cout << "   0|1|2|3|4|5|6|7|8|9" << endl << endl;
    for(int i=0; i<10; i++)  //column loop
    {
        for(int j=0; j<10; j++)  //row loop
        {
            if(j==0)
            {
                cout << i << "  " ; //print row number and spaces before new row
            }
            cout << grid[i][j] ;
            if(j!=9)
            {
                cout << "|";
            }
        }
    cout << endl; //new line at end of column
    }
}


My indexes are inbounds, any help appreciated!
Indices start at 0. If you declare an array with 9 elements, the indices go from 0-8. In your code try to access the elements from 0-9 instead, which causes a segfault.
Uhh you're going out of the bounds of the array...
Where am I going out of bounds? In the declaration or the loops that print the values?
If you want a 10x10 array you should do this -> int grid[10][10];
Solved! Thanks all for the speedy response.
Topic archived. No new replies allowed.