Understanding 2D Arrays

Alright guys. Here it goes. I just can't absorb the concepts of 2D arrays. Just don't know where I encounter a problem. Just 2D arrays as a whole. Can please make me understand the concept of multidimensional arrays. And please not that tic-tac-toe example! It hurts my mind even to look at it!
See if this helps:

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
40
41
42
43
44
45
#include <iostream>

int main()
{
    using array_1d = int[3] ; // type array_1d is an alias for 'array of 3 int'
    typedef int array_1d[3] ; // same as above

    using array_2d = array_1d[4] ; // type array_2d is an alias for 'array of 4 array_1d'
    typedef array_1d array_2d[4] ; // same as above
    using array_2d = int[4][3] ; // same as above
    typedef int array_2d[4][3] ; // same as above

    // a1, a2, a3 all have the same type: 'array of 4 array_1d'
    array_2d a1 = { {10,11,12}, { 13,14,15}, {16,17,18}, {19,20,21} } ;
    array_1d a2[4] = { {10,11,12}, { 13,14,15}, {16,17,18}, {19,20,21} } ;
    int a3[4][3] = { {10,11,12}, { 13,14,15}, {16,17,18}, {19,20,21} } ;

    // each element of array_2d is of type 'array_1d' or 'array of 3 int'
    for( array_1d& row : a1 ) // for each element of array a1
    {
        for( int v : row ) // for each int in the array 'row' (of type 'array of 3 int')
            std::cout << v << ' ' ;

        std::cout << '\n' ;
    }

    std::cout << "--------------\n" ;

    for( auto& row : a2 ) // for each element of array a2 (type is deduced)
    {
        for( int v : row ) // for each int in the array row (of type 'array of 3 int')
            std::cout << v << ' ' ;

        std::cout << '\n' ;
    }

    array_1d* p1 = a3 ; // implicit conversion from array to pointer to first element
    int(*p2)[3] = a3 ; // same as above
    array_1d* p3 = &( a3[0] ) ; // same as above

    int* p4 = a3[1] ; // implicit conversion from array to pointer to first element
    int* p5 = &(a3[1][0]) ; // same as above
    p4[1] = 0 ; // subscript operator on pointer
    a3[1][1] = 0 ; // same as above
}
I lost you after you started the for loop. Sorry but my C++ is not that advanced.
> I lost you after you started the for loop.

The same code, with classical for-loops:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

int main()
{
    using array_1d = int[3] ; // type array_1d is an alias for 'array of 3 int'
    typedef int array_1d[3] ; // same as above

    using array_2d = array_1d[4] ; // type array_2d is an alias for 'array of 4 array_1d'
    typedef array_1d array_2d[4] ; // same as above
    using array_2d = int[4][3] ; // same as above
    typedef int array_2d[4][3] ; // same as above

    // a1, a2, a3 all have the same type: 'array of 4 array_1d'
    array_2d a1 = { {10,11,12}, { 13,14,15}, {16,17,18}, {19,20,21} } ;
    array_1d a2[4] = { {10,11,12}, { 13,14,15}, {16,17,18}, {19,20,21} } ;
    int a3[4][3] = { {10,11,12}, { 13,14,15}, {16,17,18}, {19,20,21} } ;


    for( int i = 0 ; i < 4 ; ++i ) // for each position in array a1
    {
        array_1d& row = a1[i] ; // each element of array_2d is of type 'array_1d' or 'array of 3 int'

        for( int j = 0 ; j < 3 ;  ++j ) // for each position in the array 'row' (which is of type 'array of 3 int')
        {
            int& value = row[j] ; // each element of row is of type int
            std::cout << value << ' ' ;
        }

        std::cout << '\n' ;
    }

    std::cout << "--------------\n" ;

    // another way of writing the above loop
    for( int i = 0 ; i < 4 ; ++i ) 
    {
        for( int j = 0 ; j < 3 ;  ++j )  
        {
            // a2[i] is the sub-array of a2 at position i
            // a2[i][j] is the int at position j of that sub-array
            std::cout << a2[i][j] << ' ' ; // print out the value
        }

        std::cout << '\n' ;
    }

    array_1d* p1 = a3 ; // implicit conversion from array to pointer to first element
    int(*p2)[3] = a3 ; // same as above
    array_1d* p3 = &( a3[0] ) ; // same as above

    int* p4 = a3[1] ; // implicit conversion from array to pointer to first element
    int* p5 = &(a3[1][0]) ; // same as above
    p4[1] = 0 ; // subscript operator on pointer
    a3[1][1] = 0 ; // same as above
}
What is int& value = row [j] ; supposed to mean? What are you doing with the memory address? Sorry I haven't covered pointers (some say its the soul of C++), just a brief readthrough.
> What is int& value = row [j] ; supposed to mean?

int& signifies that the variable is a 'reference to int'; it is an alias for an int

int& value = row [j] ; 'value' is another name by which we can refer to the int at position j in the array row

http://www.cprogramming.com/tutorial/references.html
@JLBorges
It is now as clear as a day! Thanks for all the energy you expended on my near hopeless case haha! Buuut I understand 2D arrays now!
Topic archived. No new replies allowed.