owner sign in

4.11 C multidimensional arrays

C programming language!arrays!multidimensional

In C, an array that contains other arrays is called a multidimensional array. The familiar mathematical object called a "matrix" can be represented by a multidimensional array. Consider the \(2\times4\) matrix \[\begin{aligned} \mat{A} = \begin{bmatrix} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \end{bmatrix}. \end{aligned}\] In C, a \(2\times4\) multidimensional array (a two-array of four-arrays) representing \(\mat{A}\) can be initialized as

int A[2][4] = {
  {1,2,3,4},
  {5,6,7,8}
};

The elements of A can be accessed individually with A[i][j], where i and j are zero-indexed array index integers. Furthermore, assignment to index i,j uses the syntax

This is a preview. Read the full book in print or ebook — get it from MIT Press (ISBN 9780262548762).

Instructor with access? Instructor login.