Two Dimensional Array in C

The two-dimensional array can be defined as an array of arrays.A two-dimensional array can be used to represent a matrix, a table or board games (Sudoku). The row and column positions are given as successive indices. When you declare a variable of such an array, use a pair of square brackets for each dimension.

Declaration of Two Dimensional Array :

The syntax of 2D array declaration is given below.

Data_type Array_name[Rows][Columns];

Consider the following example.

int Twodimen[4][3];

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

Initilization of Two Dimensional Array :

In the 1D array, we don’t need to specify the size of the array if the declaration and initialization are being done simultaneously. However, this will not work with 2D arrays. In 2D array, we will have to define at least the second dimension of the array. The two-dimensional array can be declared and defined in the following way.

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

Example of Two Dimensional Array in C:

To print all the elements of a two dimension array we use a doubly-nested for-loop. In the following example, there are 3 rows and 2 columns.

#include <stdio.h>
  int row,col;
  int table[3][2] = { {10, 22}, {33, 44}, {45, 78} };
  for (row = 0; row < 3; row++)
  for (col = 0; col < 2; col++)

Output :

Leave a Comment

Your email address will not be published. Required fields are marked *

// Sticky ads