Multi-dimensional arrays
Multi-dimensional arrays in C programming allow you to store array data types in more than one dimension. Here multi-dimensional arrays involve storing, displaying or processing array elements in a table or matrix form. Where essentially declaring array elements is a matrix or table-like structure format of variable elements.
So let’s see how we can declare, initialize, access and process multi-dimensional arrays in C language.
Multi-dimensional array declaration in C language.
Multi-dimensional array in C program is declared by defining multiple sets of square brackets [ ] where each set represents a different dimensional row and column order of the array. Where these 3d or multi-dimensional arrays are stored in multiple row column format.
Multi-dimensional array example.
datatype arrayname[arraysize1][arraysize2]…[numberofsizeN];
datatype array – In datatype array you specify the default data type of array elements. For example, (int, char, double, long double etc.)
arrayname – Array name is the program identifier used to refer to the declared array variable name.
arraysize1, arraysize2, …, arrayofsizeN – Here you specify the size of each dimension of the declared array in the C program. This declared array should be in a constant integer format.
Initialization of Multi-dimensional array in C program.
In C program, multi-dimensional array is initialized at the time of declaration similar to one-dimensional array. Here you are given an example of initializing a simple 2 dimensional array in C program.
int table[3][3] = {
{3 , 1, 4},
{9, 2, 7},
{5, 6, 8}
};
The above example shows a table format consisting of a 3×3 array row column of integers initialized with array values declared as the table name. Where each multi-dimensional row is declared within curly braces {}, and stored array integer elements separated by the comma operator.
Accessing elements in multi-dimensional arrays in C.
Accessing array elements in a multi-dimensional array involves specifying the array index memory location for each dimension within the square brackets [ ].
To access an element in a multi-dimensional array matrix/table array.
int arrayvalue = table[1][2]; // Accesses the element table array with row 1, column 2 output value is – 7
Here in multi-dimensional array, table[1][2] displays the array element from the memory address by accessing the array element in the second row ([1]) and third column ([2]).
Totaling 2 Dimensional Array Elements in C Program.
So let’s add the array elements in a two dimensional array in row and column format. Here table totals all array elements in a 3×3 table matrix.
#include <stdio.h>
int main() {
int table[3][3] = {
{3 , 1, 4},
{9, 2, 7},
{5, 6, 8}
};
int total = 0;
//calculate the total of all elements in the table matrix
for (int p = 0; p < 3; p++) { // move in for Loop through the p variable rows
for (int q = 0; q < 3; q++) { // move in for Loop through the q variable columns
total += table[p][q];
}
}
printf(“\n total of all table matrix elements – %d”, total);
return 0;
}
Passing a multi-dimensional array to a function.
While passing a multi-dimensional array to a function in a C program, all array dimensions except the first one must be defined in the function parameter list for proper array type matching.
So let us explore a multi-dimensional array in C program that prints the elements of a 2 dimensional array in C to the console screen.
void printtable(int rows, int cols, int table[rows][cols]) {
for (int p = 0; p < rows; p++) {
for (int q = 0; q < cols; q++) {
printf(“%d “, table[p][q]);
}
printf(“\n”);
}
}
int main() {
int table[3][3] = {
{3 , 1, 4},
{9, 2, 7},
{5, 6, 8}
};
// Calling the function to print the table matrix elements
printtable(3, 3, table);
return 0;
}
In the above 2 dimensional array example, the printtable 2d matrix is defined to store 2D array elements with a specific dimensional rows and columns. While calling printtable, the array table of dimension 3 and 3 are passed as argument values from the main() function.