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.
#include<stdio.h> int main() { int a[][][2] = { {{1, 2}, {3, 4}}, {{5, 6}, {7, 8}} }; // error printf("%d", sizeof(a)); getchar(); return 0; } |
chevron_right
filter_none
Following 2 programs work without any error.
// Program 1 #include<stdio.h> int main() { int a[][2] = {{1,2},{3,4}}; // Works printf("%lu", sizeof(a)); // prints 4*sizeof(int) getchar(); return 0; } |
chevron_right
filter_none
// Program 2 #include<stdio.h> int main() { int a[][2][2] = { {{1, 2}, {3, 4}}, {{5, 6}, {7, 8}} }; // Works printf("%lu", sizeof(a)); // prints 8*sizeof(int) getchar(); return 0; } |
chevron_right
filter_none
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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Initialization of variables sized arrays in C
- Multidimensional Arrays in C / C++
- Initialization of data members
- Initialization of static variables in C
- Initialization of global and static variables in C
- Implicit initialization of variables with 0 or 1 in C
- Uniform Initialization in C++
- Zero Initialization in C++
- Multidimensional Pointer Arithmetic in C/C++
- How to print dimensions of multidimensional array in C++
- Variable Length Arrays in C and C++
- How to concatenate two integer arrays without using loop in C ?
- Arrays in C/C++
- How arrays are passed to functions in C/C++
- Arrays in C Language | Set 2 (Properties)
- C | Arrays | Question 1
- C | Arrays | Question 2
- C | Arrays | Question 4
- C | Arrays | Question 5
- C | Arrays | Question 6

