Two Dimensional Arrays in C

For Complete YouTube Video: Click Here

In this class, we will try to understand Two Dimensional Arrays in C.

We have clearly understood arrays and array initialization in our previous classes.

Two Dimensional Arrays

An array of arrays is known as a two-dimensional array.

Declaration of Two Dimensional array

The image below shows how to declare two-dimensional arrays.

Two Dimensional Arrays in C Declaration
Two Dimensional Arrays in C Declaration

In comparison to the one-dimensional array, the two-dimensional array has two enclosed square brackets, representing the number of rows and the other for the number of columns.

Visualization of a two-dimensional array

We can visualize the concept of a two-dimensional array by a table or by a matrix.

We will try to visualize by using a matrix.

The image below is the visualization of an array using a matrix.

Two Dimensional Arrays in C Visualization as Matrix
Two Dimensional Arrays in C Visualization as Matrix

The above image has two rows and three columns.

The indexes for the rows are zero and one.

Whereas for columns, the indexes are zero, one, and two.

Each box in the above image is for an element of the array.

With a matrix of 2*3, we can have six elements.

Initialization of a two-dimensional array

The image below is how the two-dimensional can be visualized.

Two Dimensional Arrays in C Initialization
Two Dimensional Arrays in C Initialization

In the above initialization, the compiler will initialize the first flower braces in the first row, and the second flower braces will be initialized in the second row.

The image below shows the visualization of the two-dimensional array after initialization.

Two Dimensional Arrays in C After Initialization
Two Dimensional Arrays in C After Initialization

Accessing the values of a two-dimensional array

For example, if we want to access the value two in the first row.

The image below shows the declaration to access a value in the two-dimensional array.

Accessing Elements of a Two Dimensional Arrays in C
Accessing Elements of a Two Dimensional Arrays in C

In the above declaration, the row value zero represents the row in which the value is stored, and the column value one represents the column in which the value is stored.

Two-dimensional Arrays and Memory

How will the values get stored in the memory?

Do we have a two-dimensional memory?

No.

The C Compiler uses any of the two ways to store the values in a two-dimensional array.

  1. Row Major Order
  2. Column Major Order

Row Major Order

If the element of the array is stored row-wise, then it is called row-major order.

The image shows the storage of two-dimensional array elements using ‘row-major order.

Two Dimensional Arrays in C Row Major Order
Two Dimensional Arrays in C Row Major Order

Column Major Order

If the element of the array is stored column-wise, then it is called column-major order.

The image shows the storage of two-dimensional array elements using ‘column-major order.

Two Dimensional Arrays in C Column Major Order
Two Dimensional Arrays in C Column Major Order