previous | start | next

11. Two Dimensional Arrays

Two dimensional arrays logically consist of rows and columns. For example, an array with 3 rows and 4 columns:

        1  2  3  4
        5  6  7  8
        9 10 11 12
     

A C++ two dimensional int array with 3 rows and 4 columns can be declared like this:

        int a[3][4];

or with initialization
    
        int a[][4] = 
        {
          {1, 2, 3, 4},
          {5, 6, 7, 8},
          {9, 10, 11, 12}
        };


     

What is the scale factor for computing a + 1? That is what address is represented by a + 1?

Hint: In memory a two dimensional array is stored one row after another. So the 3 rows of a are stored as shown below. So a + 1 should be the address of the first element of the second row, namely the 5, which is 4 integers (or 16 bytes) from 1.

Each row has 4 integers and so the size of each row in bytes is 16.

        1 2 3 4 5 6 7 8 9 10 11 12
     


previous | start | next