previous | start | next

Array Access Formula for a 2-dimensional Array

Two dimensional arrays in C are stored in row-major order:

 int a[4][5];      
   
Row 0 Row 1 Row 2 Row 3
                                       

The last element of row 0 is a[0][4].       

The next element in memory is the first element of row 1: a[1][0]        .

  address(a[i][j]) = (number of rows before row i) * sizeof(a row) +
                     (number of elements before a[i][j] in row i) * sizeof(element)      
   

So for this int array with 4 row and 5 columns

  address(a[i][j]) = i * (5 * size(int)) + j * sizeof(int)
                   = i * 20 + j * 4
   


previous | start | next