×
☰ Menu

Two Dimensional Array in C

An array of arrays can be defined as a two-dimensional array. The matrices that make up the 2D array are represented as a collection of rows and columns. 2D arrays, on the other hand, are used to build a data structure that resembles that of a relational database. It makes it simple to store large amounts of data at once, which may then be supplied to any number of functions as needed.

Declaration of two dimensional Array in C

The syntax to declare the 2D array is given below.

data_type array_name[rows][columns]; 

Consider the following example.

int a[3][3]; 

Here, 3 is the number of rows, and 3 is the number of columns.

 

Initialization of 2D Array in C

 

int a[3][3] = {  
   {0, 1, 2} ,   /*  initializers for row indexed by 0 */
   {3, 4, 5} ,   /*  initializers for row indexed by 1 */
   {6,7, 8}   /*  initializers for row indexed by 2 */
};

The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to the previous example −

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

Accessing Two-Dimensional Array Elements

An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example −

int value = a[2][3];