C language permits the use of multidimensional arrays. It is easy to visualize arrays up to three dimensions. However, we may have difficulty in visualizing a four, five or in general, an n-dimensional array.
A four-dimensional array can be thought of as a one-dimensional array in which each element is a three-dimensional array or as a matrix in which each element itself is a matrix or even as a three dimensional array having one-dimensional arrays as its elements.
Consider for example a school having six classes (5 to 10) each having up to three divisions (A, B and C). Each division can have up to 60 students and in any particular examination, each student has to appear for up to 10 subjects depending on his/her class. ยท
Consider now that we wish to store the marks scored by students in a particular examination. The marks obtained by a student can be stored in a vector of size 10, whereas we require a matrix of size 60 x 10 to store the marks of all the students in any division and a three-dimensional array of size 3 x 60 x 10 to store the marks of all the students in a particular class. Now how can we store the marks of all the students in the school? By extending the above discussion, we can use a four-dimensional array of size 6 x 3 x 60 x 10. This array can now be considered as six arrays of size 3 x 60 x 10 or as a matrix of size 6 x 3 whose individual elements are matrices of size 60 x 10 or even as a three-dimensional array of size 6 x 3 x 60 where each element is a vector of size 10.
We can extend this discussion further and use a five-dimensional array to store the data of all the examination conducted by this school in an academic year and a six-dimensional array to store the data for several years.
The declaration of an n-dimensional array takes the following form:
arr_type arr_name [size1] [size2] ... [size_n] ;
where arr_ name is the name of the array being declared, each element of which is of type arr_type. The expressions size 1, size2, ... , size_n enclosed in square brackets are integral constants or constant integral expressions that specify the number of elements in each dimension of the array.
As with vectors and matrices, we can declare multidimensional arrays of any built-in or user-defined type and the array elements are numbered starting from 0 (and not from 1) in each dimension.
An element of a multidimensional array can be accessed by writing the array name followed by subscript expressions within subscript operators as shown below.
arr_name [exprl] [expr2] ... [expr_n]
We can perform various operations on the elements of multidimensional arrays, similar to those discussed in one- and two-dimensional arrays. Thus, for an array of a built-in type, we can use an array element in an expression, assign a value to it, perform increment and decrement operations on it, pass it as an argument to a function and so on.
As with one- and two-dimensional arrays, C language does not provide any statement or operator to perform operations on the entire multidimensional array. Nested for loops are usually used to perform operations on an entire array as illustrated below for a three dimensional array a of size m x n x p:
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
for (k = O; k < p; k++) {
/*process element a[i][j][k] */
}
}
}
C language allows the initialization of multidimensional arrays using a syntax similar to that used for initialization of vectors and matrices. First, we can initialize them using a single comma separated list enclosed in braces as follows
data_type arr_name[size1] [size2] ... [size_n] = {expr1, expr2, ...};
As with vectors and matrices, the initialization list may be empty causing all the array elements to be initialized to default values which is 0 for arithmetic types.
We can also use a nested syntax similar to that used for the initialization of a matrix. For example, a three-dimensional array of size 2 x 3 x 4 can be initialized as shown below.
int a[2] [3] [4] = {
{
{100, 101, 102, 103},
{l10, 111 , 112, 113},
{120, 121, 122, 123}
} ,
{
{200, 201, 202, 203},
{210, 211, 212, 213},
{220, 221, 222, 223}
}
};
Note that we can omit the first array dimension (2) in the above initialization. Also, we can specify fewer elements for any row or fewer rows for any matrix. In such cases, the remaining elements in that row or matrix will be initialized to default values. Consider for example, the initialization statement given below.
int b[2] [3] [4] = {{{l, 2},{3, 4, 5)}};
This statement initializes array bas shown below.
C language allows multidimensional arrays to be passed as actual arguments to a function. Most of the discussion about two-dimensional arrays in the previous section is applicable to multidimensional arrays as well. Thus, while declaring a multidimensional array as a function parameter, we may omit the first array dimension only. All the remaining array dimensions must be specified. In the calling functions, the size of the argument array must match these specified array dimensions.
Note that as with vectors and matrices, we can pass the actual size of the array to be processed using additional parameters to this function. Also, recall that all the arrays including multidimensional arrays are passed by reference.
Consider the function definition given below that accepts an integer array of size M x 10 x 10 and three parameters m, n and p that specify the actual size of the array to be processed.
void func(int arr[][10][10], int m, int n, int p)
{
/* process elements of array arr */
}
Some calls to this function are shown below.
int main()
{
int a[10] [10] [10], b[5] [10] [10], c[50] [10] [10], d[10] [10] [5];
func(a, 10, 10, 10); /* ok */
func(b, 3, 4, 5); /* ok */
func(c, 25, 10, 8); /* ok */
func(d, 5, 5, 5);
func(d, 10, 4, 5);
}
Illustrates initialization of multi-dimensional arrays
#include <stdio.h>
void main()
{
int i,j;
int San[3] [4] = {{1, 2, 3, 4}, {6, 7, 8, 9}, { 3, 5, 7, 9}};
int Din [3] [4] = {{1, 2 },{6,7,8}, { 7}};
//The above initialization contains fewer elements.
int Dal [3] [4] = {1,2,3,4,11,12,13,14,21};
// The above initialization also has fewer elements.
clrscr();
for(i=0;i<3; i++)
{
for (j =0; j<4; j++)
{
printf("\nSan[%d] [%d] = %d\t", i, j, San[i] [j]);
printf("Din[%d] [%d] = %d\t Dal[%d] [%d] = %d ", i, j, Din[i] [j], i,j, Dal [i] [j] ) ;
}
printf ("\n");
}
}
Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.
Related Articles
Basic Courses
Advance Courses