How do I initialize a 2D array with 0s when I declare it?
double myArray[3][12] = ?
Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces. Ex: 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.
Like the one dimensional array, 2D arrays can be initialized in both the two ways; the compile time initialization and the run time initialization. int table-[2][3] = { { 0, 2, 5} { 1, 3, 0} }; This way is the best way to initialize the 2D array. It also increases the readability of the user.
double myArray[3][12] = {0};
or, if you want to avoid the gcc warning "missing braces around initializer" (the warning appears with -Wall
or, more specifically -Wmissing-braces
)
double myArray[3][12] = {{0}};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With