Filling array

Hi, I created the following code into netbeans 8.0 to test my concepts:

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
int main(int argc, char** argv) {    
    int mat[10][10],i,j;    
    for(i = 0; i < 10; i++)
        for(j = 0; j < 10; j++)
            mat[i][j] = 0;
    
    for(i = 0; i < 10; i++){
        for(j = 0; j < 10; j++){
            cout << mat[i][j] << " ";
        }
        cout << "\n";
    }
    
    mat[0][9] = 1;
    mat[0][10] = 1;
    mat[0][11] = 1;
    
    cout << "\n\n";
        
    for(i = 0; i < 10; i++){
        for(j = 0; j < 10; j++){
            cout << mat[i][j] << " ";
        }
        cout << "\n";
    }
    return 0;
}

I thought the program would fill mat[0][9] with the value 1 and the other positions would give some kind of error. But when I ran the program, the mat[0][9] position was filled with the value 1, after he met the first two elements of the second row with the value 1 also. Do not understand why he moved to the second line, the ones he should be filled mat[1][0] and mat[1][1]. Can anyone help me understand what happened? Thanks!
Remember that the way you created your array the array elements are contiguous in memory, so mat[1][0] follows mat[0][9]. But beware when you write past the end of the array (mat[9][10]) you would be in undefined behavior land.

I did a test showing the address of the position [0] [10] and position [1] [0] and the addresses were the same. It seems that once used a compiler that did this check, do not remember if it was in the old Turbo C or if it was a Pascal compiler. But it really was what you said, the matrix is, physically, a sequence of bytes. Representation in row and column is only a logical representation. Thanks jib!
Topic archived. No new replies allowed.