Understanding Multidimensional Array in C
In C programming, arrays are commonly used data structure that allow the storage of multiple values in a single variable. A multidimensional array in C is basically an array of arrays. In this article, we will learn how to initialize and work with multidimensional arrays, especially 2D arrays, in C using various examples. So, without further ado, let’s get started!
In this article, we will be discussing the multidimensional array in C.
Table of Contents
- What is Multidimensional Array in C?
- Initializing a Multidimensional Array
- Accessing Elements of a 2D Array
- Changing Elements of a 2D Array
- Looping Through a 2D Array
What is Multidimensional Array in C?
A multidimensional array in C programming is an array of arrays. It is a collection of elements that are organized in a two or more dimensional structure. Each element in a multidimensional array is identified by a set of indices, which represent the position of the element in the array.
In C, you can create multidimensional arrays by declaring an array with multiple sets of brackets. For example, the following code declares a two-dimensional array with three rows and four columns:
int array[3][4];
In this example, the first set of brackets indicates the number of rows in the array, and the second set of brackets indicates the number of columns in each row. You can also create arrays with more than two dimensions by adding additional sets of brackets.
To access the element in a multidimensional array, you use the indices of the element. For example, to access the element in the second row and third column of the array declared above, you would use the following code:
int element = array[1][2];
Note that the indices in a multidimensional array are zero-based, meaning that the first element in each dimension is numbered 0, not 1.
Explore free C programming courses
Initializing a Multidimensional Array in C
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
Initialization of a 2D Array
In C programming, you can initialize a 2D array by specifying the initial values for each element in the array. There are several ways to do this, including:
- Initializing the array with a brace-enclosed list of values:
int array[2][3] = {{1, 2, 3}, {4, 5, 6}};
This initializes a 2D array with 2 rows and 3 columns. The 1st row contains the values 1, 2, and 3, and 2nd row contains 4, 5, and 6 values.
- Initializing the array with a brace-enclosed list of values for each row:
int array[2][3] = { {1, 2, 3}, {4, 5, 6} };
This initializes the same array as above, but in a more compact way.
- Initializing the array with a nested for loop:
int array[2][3];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
array[i][j] = i * 3 + j + 1; }}
This initializes the same 2D array as above by using a nested for loop to set the value of each element in the array.
- Initializing the array with a combination of both:
int array[2][3] = { {1, 2, 3}, {0} };
This initializes a 2D array with 2 rows and 3 columns. The 1st row contains the values 1, 2, and 3, and the 2nd row contains 0 value for all columns.
- Initializing the array using designated initializers (C99 or later):
int array[2][3] = { [0] = {1, 2, 3}, [1] = {4, 5, 6} };
This initializes the same 2D array as the first example, but using designated initializers to explicitly set the values for each row.
Note that when initializing a 2D array, you must specify number of columns and rows in an array.
Initialization of a 3D Array
You can initialize a 3D array using similar methods as above. Let’s see how:
There are several ways to do this, including:
- Initializing the array with a brace-enclosed list of values:
int array[2][3][4] = {{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}},
{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}};
This initializes a 3D array with two “layers”, each of which contains three rows and four columns. Each element in the array is specified by three indices: the layer index, the row index, and the column index.
- Initializing the array with a brace-enclosed list of values for each layer:
int array[2][3][4] = {{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}},
{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}};
This initializes the same 3D array as above, but in a more compact way.
- Initializing the array with nested loops:
int array[2][3][4];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
array[i][j][k] = i * 12 + j * 4 + k + 1;
}
}
}
This initializes the same 3D array as above by using nested loops to set the value of each element in the array.
- Initializing the array with a combination of both:
int array[2][3][4] = {{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}, {0}};
This initializes a 3D array with two “layers”, each of which contains three rows and four columns. The first layer is initialized with the values 1 through 12, and the second layer is initialized with 0 for all elements.
- Initializing the array using designated initializers (C99 or later):
int array[2][3][4] = { [0] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} },
[1] = { {13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24} } };
This initializes the same 3D array as the first example but uses designated initializers to explicitly set the values for each layer, row, and column.
Note that when initializing a 3D array, you must specify the number of layers, rows, and columns in the array.
Accessing Elements of a 2D Array
You can access the elements of a 2D array by using two indices: one for the row and one for the column.
The syntax for accessing an element in a 2D array is as follows:
array[row_index][column_index]
Here’s an example that initializes a 2D array and then accesses its elements:
#include \n \n \n <stdio.h>\n \n \n \n \n \n \n \n \n int main() {\n \n \n \n \n \n int array[3][4] = {{1, 2, 3, 4},\n \n \n \n \n \n {5, 6, 7, 8},\n \n \n \n \n \n {9, 10, 11, 12}};\n \n \n \n \n \n // Access the element at row 1, column 2\n \n \n \n \n \n int element = array[1][2];\n \n \n \n \n \n printf("Element at row 1, column 2: %d\n", element);\n \n \n \n \n \n // Change the value of the element at row 0, column 3\n \n \n \n \n \n array[0][3] = 99;\n \n \n \n \n \n printf("New value of element at row 0, column 3: %d\n", array[0][3]);\n \n \n \n \n \n return 0;\n \n \n \n \n \n }\n \n \n \n \n \n \n \n \n </stdio.h>
Output:
Element at row 1, column 2: 7New value of element at row 0, column 3: 99
In this example, the 2D array array is initialized with the values 1 through 12. We then access the element at row 1, column 2 (which has the value 7), and print its value. We also change the value of the element at row 0, column 3 to 99, and print its new value. Note that the indices for a 2D array in C are zero-based, meaning that the first row or column has an index of 0, not 1.
Changing Elements of a 2D Array
In C programming, you can change the elements of a 2D array by using the same syntax as accessing the elements. Here’s an example that shows how to change elements in a 2D array:
#include \n \n \n <stdio.h>\n \n \n \n \n \n \n \n \n int main() {\n \n \n \n \n \n int array[3][4] = {{1, 2, 3, 4},\n \n \n \n \n \n {5, 6, 7, 8},\n \n \n \n \n \n {9, 10, 11, 12}};\n \n \n \n \n \n // Change the value of the element at row 1, column 2\n \n \n \n \n \n array[1][2] = 99;\n \n \n \n \n \n printf("New value of element at row 1, column 2: %d\n", array[1][2]);\n \n \n \n \n \n // Change the value of all elements in row 0\n \n \n \n \n \n for (int i = 0; i < 4; i++) {\n \n \n \n \n \n array[0][i] *= 2;\n \n \n \n \n \n }\n \n \n \n \n \n // Print the new values of all elements in row 0\n \n \n \n \n \n printf("New values of elements in row 0: ");\n \n \n \n \n \n for (int i = 0; i < 4; i++) {\n \n \n \n \n \n printf("%d ", array[0][i]);\n \n \n \n \n \n }\n \n \n \n \n \n printf("\n");\n \n \n \n \n \n return 0;\n \n \n \n \n \n }\n \n \n \n \n \n </stdio.h>
Output:
New value of element at row 1, column 2: 99New values of elements in row 0: 2 4 6 8
In this example, we first initialize a 2D array array with the values 1 through 12. We then change the value of the element at row 1, column 2 to 99. We print the new value of this element to confirm that it has been changed.
Next, we change the value of all elements in row 0 by using a for loop to iterate over the columns in row 0 and multiply each element by 2. We then print the new values of all elements in row 0 to confirm that they have been changed.
Note that changing the value of an element in a 2D array does not affect the other elements in the array.
Looping Through a 2D Array
In C programming, you can loop through a 2D array using nested loops. Here’s an example:
#include \n \n \n <stdio.h>\n \n \n \n \n \n \n \n \n int main() {\n \n \n \n \n \n int array[3][4] = {{1, 2, 3, 4},\n \n \n \n \n \n {5, 6, 7, 8},\n \n \n \n \n \n {9, 10, 11, 12}};\n \n \n \n \n \n // Loop through the 2D array and print all elements\n \n \n \n \n \n for (int i = 0; i < 3; i++) {\n \n \n \n \n \n for (int j = 0; j < 4; j++) {\n \n \n \n \n \n printf("%d ", array[i][j]);\n \n \n \n \n \n }\n \n \n \n \n \n printf("\n");\n \n \n \n \n \n }\n \n \n \n \n \n return 0;\n \n \n \n \n \n }\n \n \n \n \n \n \n \n \n </stdio.h>
Output:
1 2 3 45 6 7 89 10 11 12
In this example, we first initialize a 2D array with the values 1 through 12. We then use nested for loops to iterate over each row and column of the array. The outer loop iterates over the rows (i.e., the first dimension of the array), and the inner loop iterates over the columns (i.e., the second dimension of the array).
Inside the inner loop, we access each element of the array using the indices i and j, and print its value using the printf function. We also print a newline character after each row to format the output.
Note that the order of the loops is important: the outer loop should iterate over the rows and the inner loop should iterate over the columns, as shown in the example. If you switch the order of the loops, you will iterate over the columns first and the rows second, which may not give you the desired output.
Endnotes
Hope that this article on multidimensional array in C was helpful for you to understand how to initialize and use multidimensional arrays in C. If you want to learn more about C programming and solidify your basics, you can explore our articles on C.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio