Understanding Multidimensional Array in C

Understanding Multidimensional Array in C

6 mins read197 Views Comment
Updated on Mar 14, 2023 14:31 IST

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!

2023_03_Difference-between-Money-Market-and-Capital-Market-3.jpg

In this article, we will be discussing the multidimensional array in C.

Table of Contents

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];
Copy code

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];
Copy code

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

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4.24 K
6 weeks
475
6 weeks
– / –
45 hours
4 K
80 hours
– / –
1 month
– / –
40 hours
– / –
2 months
– / –
1 year
4 K
80 hours

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}};
Copy code

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} };
Copy code

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;
}
}
Copy code

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} };
Copy code

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} };
Copy code

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}}};
Copy code

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}}};
Copy code

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;
}
}
}
Copy code

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}};
Copy code

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} } };
Copy code

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]
Copy code

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>
Copy code

Output:

 
Element at row 1, column 2: 7
New value of element at row 0, column 3: 99
Copy code

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>
Copy code

Output:

 
New value of element at row 1, column 2: 99
New values of elements in row 0: 2 4 6 8
Copy code

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>
Copy code

Output:

 
1 2 3 4
5 6 7 8
9 10 11 12
Copy code

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.

Multidimensional Arrays in C++
Multidimensional Arrays in C++
A multi-dimensional array can be termed as an array of arrays that stores homogeneous data in tabular form. This article covers Multidimensional Arrays in C++ with working examples. It also...read more
How to use For Loop in C++
How to use For Loop in C++
Loops are used to repeat the same, or similar code number of times. In this article we will discuss how to implement for loops in c++.
Working with Strings in C++
Working with Strings in C++
Strings in C++ is a very simple yet very important topic from exams as well as interview point of view.

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.

About the Author

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