Multidimensional array in C
Array is a variable which is group of element of similar data type, it store the elements in a continuous memory location we also called as vector.
Array is also have two or more dimension of array as well, so in this tutorial we are going to cover two dimension array and multidimensional array.
Two dimension array:
Two dimensional array is also known as matrix. As we know 1D array single subscript index is used, and for 2D array two subscript index is used to store the values , to store table we use two dimensional array , as we all know table consist rows and columns so we can declare 2D array declared as
Data_type array_name [row-size] [column-size];
For example:
Int student[3][3]
In this example integer is the data type of array , student is the name of array, [3] indicate size of row is 3 and it is used for to store the record of 4 students, [3] indicate size of column is 3 consider these two column store the values, so in first column store roll no of student and second column will store marks of the students.
2-dimentional array, which contains three rows and three columns index value can be shown as below:
Example of 2D array to store employee salary record.
main()
{
int emp[4][2];
int i, j;
for (i=0;i=<3;i++)
{
printf(“Enter employee ID and Salary”);
scanf(“ %d%d”, &emp[i][0],&emp[i][1]);
}
for (i=0;i=<3;i++)
(“ \n %d%d”, &emp[i][0],&emp[i][1]);
}
In this program first for loop is used to read the values of employee id and salary and second for loop is used to print these values.
Initializing Two-Dimensional Array
Multidimensional arrays can be initialized by specifying bracketed values for each row.
int emp[4][2] = { { 21, 5000},{22, 7000},{23, 8998},{24,7898}};
By another method also we can initialize array and it is equivalent as previous
int emp[4][2] = { 21, 5000, 22, 7000, 23, 8998,24,7898};
The value is stored like one row below to other one automatically no need to specify brackets
When we initialize a 2D array it is necessary to mention column dimension and row dimension is optional.
Example of acceptable declaration
int emp[4][2] = { 21, 5000, 22, 7000, 23, 8998,24,7898};
int emp[][2] = { 21, 5000, 22, 7000, 23, 8998,24,7898};
Whereas ,
int emp[2][] = { 21, 5000, 22, 7000, 23, 8998,24,7898};
int emp[][] = { 21, 5000, 22, 7000, 23, 8998,24,7898};
Is not initializing array as column dimensions not declared.
We can also declare size of the 2-D array by using symbolic constant such as
#define ROW 3;
#define COLUMN 2;
int emp[ROW][COLUMN];
Memory allocation of 2D Array:
In above figure we have shown index value of matrix but it only conceptually true because memory doesn’t contain row and column, all type of arrays element are stored in one contiguous chain as shown below figure.
Three Dimension Array
Three Dimension Array is an array formed with arrays of array and it declare as
Data_type array_name [no_of_array] [row_size][column_size]
[no_of_array] is the outer array which hold the elements in the form of array , which is a two dimensional array of four one dimensional arrays, [row_size][column_size] declare row and column size of two dimension array.
Example:
int test[3][4][2];
data type is integer ,array name is test and it has 3 elements , each of which is a two dimensional array of size 4 row and 2 column.
In other word we can say to construct one two dimension array of 4 row and two column a four one dimensional array of two element are placed one below other and such three two dimensional array are placed one behind other to form a three dimension array.
Initialization of 3D array
int emp[3][4][2] = {
{
{ 21, 5000},
{22, 7000},
{23, 8998},
{24,7898};
}
{
{ 22,55},
{82, 7044},
{43, 894},
{24,444};
}
{
{ 21, 5040},
{82, 740},
{53, 898},
{24,788};
}
};
Arrays can be initialized by specifying bracketed values for each row so this the declaration of 3-D array of having three element.