I know that we can initialize a 2-D array with 0 by writing
a[value_1][value_2] = {0};
and if we write
a[value_1][value_2] = {number other than 0};
the first element get initialized by the number given in { } and rest all by 0.
But, the thing bugging me is that how can we initialize an array whose parameters value_1 and value_2 have been taken input from the user as it shows the error that variable sized object may not be initialized.
Also, it will be helpful if you can also tell the same about multidimensional array(instead of just 2 dimensional array).
To do this in C99 (compile with
gcc -std=c99
):In C++, variable length arrays are not supported, as @JensGustedt pointed out. On the other hand,
g++
supports it, so the same code above will work there too.As far as I know, initializers in variable sized objects are not supported, either in C or in C++.