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.
Table of Contents
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.
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.
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.
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.
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.
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.
- Row Major Order
- 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.
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.