In C programming language, arrays are classified into two types. They are as follows:
- Single Dimensional Array/One Dimensional Array
- Multi-Dimensional Array
For a single Dimensional Array, please refer article array in C. This post covers basics about Multi-Dimensional arrays. In general, 2-D arrays are used.
Multi-Dimensional Array:
An array of arrays is called as multi Dimensional Arrays or an array created with more than one dimension (size) is called as multi dimensional array.
It can be two-dimensional arrays or three-dimensional arrays or four dimensional arrays or more. The 2-D arrays are used to store data in the form of tables.
Declaration of 2-Dimensioal Array:
Syntax:
datatype array_name[rowSize][ColumnSize];
Example:
int 2D_array [2] [3];
The above declaration of two-dimensional arrays reserves 6 continuous memory locations of 2 bytes each in the form of 2 rows and 3 columns.
Note:
- We do not need to specify the size of the array in the case of 1 D array but the same is not the case in the 2D array. We must always specify the column size.
/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify the second dimension*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid because of the same reason mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }
Initialization of 2 Dimensional Arrays:
Syntax:
datatype arr-name [row_size][col_size] = {{r1c1value, r1c2value, ….}, {r2c1value,r2c2value,…}..}
Example:
int 2D_array[2][2] = { {1,2},{3,4}};
It can also be initialized as follows:
int 2D_array[2] 3] = {
{1, 2, 3},
{4, 5, 6}
};
Storing user data input into 2D array:
To store the elements entered by the user we are using two loops, one of them is a nested loop. The outer loop runs from 0 to the row-size -1 and second runs from 0 to column-size -1. The total number of elements contained is row-size * column size.
Example:
#include<stdio.h>
int main(){
/* 2D array declaration*/
int abc[5][4];
/*Counter variables for the loop*/
int i, j;
for (i=0; i<5; i++) {
for (j=0;j<4;j++) {
printf ("Enter value for abc[%d][%d]:", i, j);
scanf ("%d", &abc[i][j]);
}
}
return 0;
}
Memory Representation:
In this section, we would be seeing the conceptual memory representation and actual memory representation of two dimensional arrays.
Conceptual Memory Representation:
In the above example, there is a 2D array abc of interger type of size 20.

Actual Memory Representation:
In the case of array all the elements are stored sequentially in memory.

Accessing Individual Elements of Two Dimensional Array:
In C programming language, to access elements of a two-dimensional array we use array name followed by row index and column index value of the element to be accessed. Here row and column index values must be enclosed in separate square braces.
Syntax:
arrayName [rowIndex] [columnIndex];
Example:
2Darray [0] [1] =10;
In the above statement, the element with row index 0 and column index 1 of array is assigned value 10.
Pointers and 2D array:
In the case of a one-dimensional array name, it works as a pointer to the base element (first element) of the array. In the case of a 2D array, it works slightly differently.
For the above example, with the array name “abc”, abc[0] would have the address of the first element of the first row. Similarly abc[1] would have the address of the first element of the second row.
To access the one dimension array:
arr[i] = *(arr + i);
To access 2-D array:
arr[i][j] = *(*(arr +i) + j);
Other ways:
arr[i][j] = *(*arr + (i *NOC +j))
where NOC = no of coulumn
Categories: C Language
Leave a Reply