In C/C++, initialization of a multidimensional arrays can have left most dimension as optional. Except the left most dimension, all other dimensions must be specified.
For example, following program fails in compilation because two dimensions are not specified.
C
#include<stdio.h>
int main()
{
int a[][][2] = { {{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
printf ( "%d" , sizeof (a));
getchar ();
return 0;
}
|
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[][][2] = { {{1, 2}, {3, 4}},
{{5, 6}, {7, 8}} };
cout << sizeof (a);
getchar ();
return 0;
}
|
Following 2 programs work without any error.
C
#include<stdio.h>
int main()
{
int a[][2] = {{1,2},{3,4}};
printf ( "%lu" , sizeof (a));
getchar ();
return 0;
}
|
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[][2] = {{1, 2}, {3, 4}};
cout << sizeof (a);
return 0;
}
|
C
#include<stdio.h>
int main()
{
int a[][2][2] = { {{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
printf ( "%lu" , sizeof (a));
getchar ();
return 0;
}
|
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[][2][2] = { {{1, 2}, {3, 4}},
{{5, 6}, {7, 8}} };
cout << sizeof (a);
return 0;
}
|
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.