It is also possible for arrays to have two or more dimensions. The two dimensional array is also called as a matrix. In C language two dimensional array is represented by rows and columns. It is also known as an array of arrays or list of arrays. This two dimensional array can be called as a multi dimensional array.
A pictorial representation of the 2-D array:
The following figure shows how data is stored in memory in matrix form:
1 |
int a[3][3]={{1,2,3}, {4,5,6}, {7,8,9}}; |
Declaration of two dimensional array:
1 |
data_type array_name[size1][size2]; |
For example: int arr[4][3];
Here the first dimension always represents a row and the second dimension represents a column. In above example 4 is the row number and 3 is a column number. An arr is an array of data type int.
Initialisation of Two-Dimensional Array:
You can initialise two dimensional array in any of the following two ways:
1 2 3 4 5 |
int arr[4][3]={ {1,2,3}, {2,5,6}, {8,4,3}, {5,1,9} }; OR int arr[4][3]={1 ,2, 3, 2, 5, 6, 8, 4, 3, 5, 1, 9}; |
This example is represented in matrix form as follow:
1 2 3 4 |
1 2 3 2 5 6 8 4 3 5 1 9 |
Points to remember:
- You can skip row size. But it is mandatory to give column size.
1int a[][2] = {{1,2}, {3,4}, {5,6}};It is represented as follow:
1231 23 45 6 -
1int[3][3] = {{1}, {4}, {7}};
This is represented as follow:
1231 0 04 0 07 0 0 -
1int a[][3] = {1, 2, 3, 4, 5, 6};
It is represented as:
121 2 34 5 6 - Empty brackets are not allowed.
1int[3][3] = {{1}, {4}, {7}, {}}; //error
- Following initilisations are not valid.
- column size is not given so it is invalid.
1int xyz[][] = {{1,2}, {3,4}, {5,6}}; //error
-
1int pqr[2][]={{1,2},{3,4},{5,6}}; //error
- Variable name is not allowed as row_size or column_size:
12int row=3; column=3;int a[row][column]={{1,2},{3,4},{5,6}}; //error
-
12int arr[4][3]={1,2,3,2,5,6,8,4,3,5,1,9};printf("%d", sizeof(a)); //48 bytes
Since array is are of integer type and contain 12 elements. Therefore (4*3)*4=48 bytes(i.e number of rows * number of columns)
Accessing 2-D array elements using subscripts-
1int var = a[2][3];The 4th element from 3rd row of the array is assign to var. In this way, we can access any element from array.
Address of an element can be written as:address = *(array_name + rows x sizeof(data_type))+column x sizeof(data_type)
&a[i][j] = *(a + i) + j
& arr[0][1] = *((arr + 1 x 4)+1 x 4) = 104For example:
12345678910111213141516171819202122232425262728293031323334#include<stdio.h>int main(){int array[2][2] = {58, 96, 66, 30};//To get first elementprintf("first element is = %d", array[0][0]); //58//To get address of first elementprintf("address of first element is = %d", array); //2000printf("address of first element is = %d", (array + 0)); //2000printf("address of first element is = %d", (0+array)); //2000//To get second elementprintf("second element is = %d", array[0][1]); //96//To get address of second elementprintf("address of second element is = %d", (array + 1)); //2004printf("address of second element is = %d", (1+array)); //2004//To get third elementprintf("third element is = %d", array[1][0]); //66//To get address of third elementprintf("address of third element is = %d", (array + 2)); //2008printf("address of third element is = %d", (2+array)); //2008//To get forth elementprintf("forth element is = %d", array[1][1]); //30//To get address of forth elementprintf("address of forth element is = %d", (array + 2)); //2008printf("address of forth element is = %d", (2+array)); //2008}Example of 2-D Array in C
123456789101112131415#include<stdio.h>int main(){int i=0, j=0;int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};//traversing 2D arrayfor(i=0; i < 4; i++){for(j=0; j < 3; j++){printf("arr[%d][%d]=%d\n", i, j, arr[i][j]);}}}
Output:
123456789101112arr[0][0] = 1arr[0][1] = 2arr[0][2] = 3arr[1][0] = 2arr[1][1] = 3arr[1][2] = 4arr[2][0] = 3arr[2][1] = 4arr[2][2] = 5arr[3][0] = 4arr[3][1] = 5arr[3][2] = 6
- column size is not given so it is invalid.