Two Dimensional Array in C
Learn via video course

Overview
Dimension of an array refers to a particular direction in which array elements can be varied. An array with a single dimension is known as a one-dimensional array. An array that has a dimension greater than one is known as a multidimensional array.
For example, an array with two dimensions is a two-dimensional array. 2D array is the simplest form of a multidimensional array which could be referred to as an array of arrays. We can also have a 3D array, 4D array, and so on. C gives us the flexibility to use multidimensional arrays in our program.
Scope
This article revolves around multidimensional arrays. We shall learn how to work with a two-dimensional array in C. We'll cover pointers and 2D arrays, then move on to working with 3D arrays.
Array and its Types
An array is a data structure that contains a collection of similar types of elements stored in contiguous memory locations.
Example - Integer array is a collection of integers.
Arrays are classified into two types in C language:
- Single dimensional array or 1d array.
- Multidimensional array, e.g. - two-dimensional array in C.
In this article, we will dive deeper into the second kind of array.
Size of Multidimensional Arrays in C
It's crucial to be able to calculate the size of our array since we will need it for space complexity calculations, as well as to check how many elements are there in our array.
Let us See How We can Calculate the Size of Three Dimensional Array:
Suppose we have a three-dimensional integer array A[10][20][30].
The size of the above array is the product of the size of the dimensions, i.e. 10 * 20 * 30 = 6000 elements.
Two Dimensional Array in C
Two Dimensional Arrays can be thought of as an array of arrays or as a matrix consisting of rows and columns.
Following is an example of a 2D array:
1 | 7 | 2 |
---|---|---|
9 | 3 | 8 |
This array has rows and columns.
A two-dimensional array in C will follow zero-based indexing, like all other arrays in C.
Syntax:
Now we will review the syntax for declaring a Two-dimensional array in C.
Here i and j are the size of the two dimensions, i.e i denotes the number of rows while j denotes the number of columns.
Example:
Here we declare a two-dimensional array in C, named A which has 10 rows and 20 columns.
Initializing Two – Dimensional Array in C
We can initialize a two-dimensional array in C in any one of the following two ways:
Method 1 We can use the syntax below to initialize a two-dimensional array in C of size x * y without using any nested braces.
This will create an array of size x * y with elements being filled in the following manner:
From left to right, the first y elements will be on the first row. y + 1 onwards, the next y elements, in the order of left to right, will be filled on the second row. In this manner, all the x rows will be filled one by one.
Let us understand this with the below example:
So we will have an array of size 2 * 3 with the above initialization. Let us see how the elements will be filled:
- From left to right, the first three elements will be on the first row.
- Fourth to the last element, in the order of left to right, will be filled on the second row.
Method 2
A two-dimensional array in C can also be initialized using nested braces, which makes the visualization of each row and column a bit easier.
The syntax is as follows-
Let us see by an example how we can use nested braces to implement the above:
Each nested brace denotes a single row, with the elements from left to right being the order of elements in the columns in our 2d array.
Thus, the Number of nested braces = the Number of rows.
Accessing Elements of Two-Dimensional Array in C
Let us suppose we have an array A[10][20], and we want to access the element at row and column. Since we are using 0-based indexing, ith row will be present at position and column will be present at position.
To access an element at position (i, j), we use array_name[i - 1][j - 1]. Thus the element at the 4th row and 5th column will be accessed by A[3][4].
How to Store User Input Data into a 2D Array?
To store user input data into a 2d array, we have to traverse each row and column and fill each index one by one. For this purpose, we use two loops. The outer loop will process each row, thus it would run from 0 to the number of rows - 1. The inner loop will process each column, thus it would run from 0 to the number of columns - 1.
Eg - Suppose we have an array A of m rows and n columns. We can store the user input data into our array A in the following way:
Initially for i = 0, inner loop will traverse from j = 0 to j = n - 1. Thus for the 0th row, it will take n elements from the user. This process will be continued similarly for the second row (i = 1), third row, and so on form rows.
Pointers & 2D Array
In C, in the case of a 1D array, a single pointer can point to the first element of an array. Using that pointer, we can traverse the entire array.
For example:
Output:
Explanation: Here we saw that by using the pointer, we traversed the entire array.
Now suppose we want a pointer to point to a 2D array. We saw a single pointer can point to an entire 1D array. So all we need to do is create a pointer for each row/column of the 2D array.
Output:
Explanation: Here we used a pointer for each row and traversed the entire row using that pointer. In this way, the entire 2-d array was traversed.
Accessing Array Elements Using Pointer
Suppose we have a 2D array arr, represented this way:
Here arr points to the row of arr, which is a 1D array. Similarly (arr + 1) points to the row of arr. Hence we can represent it like:
We can say that (arr + i) points to the row of the 2D array.
Hence (arr + i) points to the address of the element of the ith row, which is a 1D array.
Now suppose we want to access the 2nd element of the 2nd row of arr. *(arr + 1) points to the 2nd row of the array. *(arr + 1) + 0 points to the 1st element of the 2nd row of the array. *(arr + 1) + 1 points to the 2nd element of the 2nd row of the array. So we can access the value of this element by: *(*(arr + 1) + 1)
Generalizing this, we can access the element at the ith row and jth column by: *(*(arr + i) + j)
So, the C code for printing each element of this array:
Output:
Explanation:
The element at the ith row and jth column of an array are equal to *(*(arr + i) + j). In this code, first, we declare and initialize the array, following which we loop over the 2D array using 2 loops, and print each element of the array using *(*(arr + i) + j). Thus the entire 2D array is printed.
Three-Dimensional Array in C
As the name suggests, a 3d array contains three dimensions, so it can be thought of as an array of 2d arrays. The three dimensions are stated as follows:
- Block Size (k)
- Row (i)
- Column (j)
Syntax
Now This is the syntax for declaring a 3-Dimensional array.
Here i, j, and k are the size of the three dimensions where i is number of blocks (2d arrays), j is number of rows and k is the number of columns.
Example (for 3D character array):
Initializing Three-Dimensional Array in C
We can initialize a 3D either by using nested braces or by mentioning the elements inside each nested brace.
Let us see an example initialization using nested brace:
Explanation:
As you can see, the nesting and number of elements depend on the dimensions of the 3D array. Since we have 2 blocks, the number of outermost inner braces is 2. Inside each of the outermost inner braces, we have 3 inner braces, since the number of rows in each block is 3. Within the innermost braces, we have 4 elements each, since our column size within each block is 4.
Let us now visualize how it with the diagram below:
Accessing Elements in Three-Dimensional Array in C
Accessing elements in the 3D array is very similar to how it is done for 2D arrays. The same zero-based indexing is used.
Let us assume the 3d array is given to us. Now suppose we want to access and print all elements of the array. The code for doing so is:
Output:
Explanation:
We use 3 nested loops to traverse the entire array and print the elements one by one.
Conclusion
- Congratulations! In this article, you learned about the array which is a collection of similar types of elements stored in a contiguous way, and its types that have different dimensions.
- A one-dimensional array stores a single list of various elements having a similar data type.
- A two-dimensional array stores an array of various arrays, a list of various lists, or an array of various one-dimensional arrays.
- A three-dimensional array is an array of 2d arrays.
- We can initialize each type of array at the time of declaration and can access any of their elements after that.