Declaration of Two Dimensional Array in C
Array and the types of an Array:
A collection of comparable types of elements kept in close proximity to one another make up an array, which is a type of data structure.
An integer array, for instance, is a collection of integers.
In the C language, arrays are divided into two types:
- 1D array stands for single-dimensional array.
- Two-dimensional array in C, for example, is a multidimensional array.
Two-dimensional Array:
A matrix of columns and rows or an array of arrays is two ways to think of two-dimensional arrays.
Here is an example of a 2D array:
1 | 5 | 6 |
4 | 9 | 8 |
Three columns and two rows constitute this array.
Like all other C arrays, a two-dimensional array will use zero-based indexing.
Syntax:
data_type_name arr_name[i][j]
Here, the two dimensions' sizes are indicated by the letters i and j, respectively, where i stands for the number of rows and j for the number of columns.
Example:
int Array_1[15][30];
Here, in the language C, we declare the two-dimensional array A, which consists of 15 rows and 30 columns.
Initialization of Two-dimensional Array in C:
In C, a two-dimensional array can be initialised using one of the two techniques given below.
Method 1:
The following syntax can be used to initialise a two-dimensional array in C that is size x * y without the need of nested braces.
int Array_name[a][b] = {element_ 1, element_ 2, ... element_ xy};
- This will produce an array of size a * b with the following elements being filled:
- The first row will include the first y components, starting from left to right. The successive y components in the second row, starting with y + 1, will be added left to right. This will insert each of the x rows one at a time.
Let's use the example below to better understand this:
int Array_name1[2][3] = {1, 8, 9, 6, 4, 7}
- With the initialization described above, we will have an array of size 2 * 3. Check out how the components are filled:
- On the first row, starting from left to right, are the first three components.
- On the second row, the fourth element will be filled in according to the order of left to right.
Method 2:
The visualisation of each row and column is a little bit easier in C because nested braces can also be used to initialise a two-dimensional array.
The following is the syntax:
int Array_name[a][b] = {{element_ 1, element_ 2, .. element_ y} , {......} , {..., element_ xy-1, element_ xy}};
Using the following example, let's examine how the previously mentioned can be implemented using nested braces:
int Array_name[2][3] = {{7, 4, 1}, {9, 6, 3}};
- The order of the elements in each nested brace's row, from left to right, corresponds to the order of the elements in the columns of our two-dimensional array.
- As a result, the number of rows equals the number of nested brackets.
Using C to Access Two-Dimensional Array Elements:
Consider that we have the array A[10][20], and we require access to the element at 4th and 5th column. As a result of our use of 0-based indexing, the ith row will be available at (i-1)th position and jth column will be available at (j-1)th position.
Utilising array_name[i - 1][j - 1] allows us to access an element at position (i, j). hence, the component at the 4th row and 5th column i.e., A[3][4] will have access to that.
How can user Input Data be Stored in a 2D Array?
- We must iterate over each row and column and fill each index one at a time in order to store user input data in a two-dimensional array. Two loops are employed for this purpose.
- Because the outer loop processes each row, it would go from 0 to the number of rows minus 1.
- Since each column will be processed by the inner loop, it would proceed from 0 to the number of columns minus 1.
Example:
Consider a columnar array A that contains m rows. In our array A, the user input data can be stored in the following ways:
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf(“ %d”, &Array_name[i][j]);
}
}
Explanation:
- In the beginning, if i = 0, the inner loop will go from j = 0 to j = n – 1. Thus, n elements will be requested from the user for the 0th row.
- The same process will be applied to the third row (i = 3), second row (i = 1), and all following form rows.
Two-dimensional Array declaration using Pointers:
Now imagine that we want a pointer to a 2D array. To build a pointer for each row and column of the 2D array is all that is required. Here, we examined each row using a pointer that was assigned to each row.
The complete 2-d array was guided through in this manner.
Example:
#include <stdio.h>
int main(void)
{
int Array_name[2][3] = {{7, 4, 1}, {9, 6, 3}};
int *ptr[2];
ptr[0] = Array_name[0]; // In a 2D array, the pointer points to the first row.
ptr[1] = Array_name[1]; // In a 2D array, the pointer points to the second row.
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", ptr[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
![Declaration of Two Dimensional Array in C](https://static.tutorialandexample.com/c/declaration-of-two-dimensional-array-in-c1.png)