Table of Contents
Initialization Of Multidimensional Arrays In C/C++
When initializing a multidimensional array, you can omit the size of the leftmost dimension, but all other dimensions must be specified. This is because the compiler can deduce the size of the leftmost dimension based on the number of elements you provide.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[][][2] = { {{1, 2}, {3, 4}},
{{5, 6}, {7, 8}} }; // error
cout << sizeof(a);
getchar();
return 0;
}
Output
./Solution.cpp: In function 'int main()':
./Solution.cpp:5:14: error: declaration of 'a' as multidimensional array must have bounds for all dimensions except the first
int a[][][2] = { {{1, 2}, {3, 4}},
^
./Solution.cpp:7:18: error: 'a' was not declared in this scope
cout << sizeof(a);
A multi-dimensional array could be initialized as follows:
Syntax
array_name[x][y] = { {a, b, c, ... }, ........., { m, n, o ...}};
The two Programs listed below work without an error
Program 1:
// Program 1
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[][2] = {{1, 2}, {3, 4}}; // Works
cout << sizeof(a); // prints 4*sizeof(int)
return 0;
}
Output
16
Program 2:
// Program 2
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[][2][2] = { {{1, 2}, {3, 4}},
{{5, 6}, {7, 8}} }; // Works
cout << sizeof(a); // prints 8*sizeof(int)
return 0;
}
Output
32
Example of Multi-dimensional Array in C++
#include <iostream>
using namespace std;
int main() {
// code
int a[2][3] = { { 1, 3, 2 }, { 6, 7, 8 } };
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
cout << "\n a[" << i << "][" << j << "]=" << a[i][j];
}
}
return 0;
}
Output
a[0][0]=1
a[0][1]=3
a[0][2]=2
a[1][0]=6
a[1][1]=7
a[1][2]=8
FAQ- Initialization Of Multidimensional Arrays in C/C++
Q1. How do you initialize multi multi-dimensional array in C?
Ans. To initialize a multidimensional array in C/C++, list the values of all elements you want to initialize. Follow the order where the compiler assigns values, increasing the subscript of the last dimension fastest. This ensures that elements are correctly assigned in memory.
Q2. What is a multidimensional array in C with example?
Ans. A multi-dimensional array is an array with more than one level or dimension. For instance, a 2D array, also known as a two-dimensional array, is essentially an array of arrays, resembling a matrix with rows and columns, similar to a table. This concept is particularly useful for organizing data in a structured way, such as spreadsheet-like data.
Q3.How do you initialize single dimensional array and multidimensional array?
Ans. Similar to one-dimensional arrays, you can initialize two-dimensional arrays by following their declaration with a list of initial values enclosed in braces. For example, int a[2][3] = {0, 0, 0, 1, 1, 1};
initializes the elements of the first row to zero and the second row to one. The initialization is done row by row, ensuring that the values are assigned correctly within the two-dimensional array structure.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together