40

How do I initialize a 2D array with 0s when I declare it?

double myArray[3][12] = ?

7 Answers 7

54
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}};
3
  • If you use the paragraph code blocks (four spaces at the beginning of line) you get syntax hightlighting, which you don't if you use inline blocks (backticks). Commented Nov 6, 2009 at 16:43
  • Is this syntax C99 or later?
    – Danijel
    Commented Oct 7, 2019 at 8:10
  • 2
    @Danijel: the {0} initializer has been valid since C89 (the 1st standardization). Don't know about before that.
    – pmg
    Commented Oct 7, 2019 at 13:37
22

If you want to initialize with zeroes, you do the following:

double myArray[3][12] = { 0 };

If you want to fill in actual values, you can nest the braces:

double myArray[3][3] = { { 0.1, 0.2, 0.3 }, { 1.1, 1.2, 1.3 }, { 2.1, 2.2, 2.3 } };
4
  • +1 for pointing out how to initialize everything ... but why did you shorten the array? :P
    – pmg
    Commented Nov 6, 2009 at 16:48
  • 15
    I shortened the array because I didn't want to type twelve sets of numbers. Commented Nov 6, 2009 at 17:15
  • 2
    You might consider using a non-square array for the second example so it's clear which dimension is the inner and which is the outer Commented Jul 13, 2020 at 20:38
  • It does not work for me, I have "error: variable-sized object may not be initialized" Commented Nov 8, 2023 at 15:11
13

The memory layout may be relevant (e.g., for serialization).

myArray[3][2] = { { 0.1, 0.2 }, { 1.1, 1.2 }, { 2.1, 2.2 } };

The first index is the row index is the slowest index. This is known as C order as opposed to F (Fortran) order.

10

pmg's method is correct, however, note that

double myArray[3][12] = {{}};

will give the same result.

Additionally, keep in mind that

double myArray[3][12] = {{some_number}};

will only work as you expect it when some_number is zero.

For example, if I were to say

double myArray[2][3] = {{3.1}};

the array would not be full of 3.1's, instead it will be

3.1  0.0  0.0
0.0  0.0  0.0

(the first element is the only one set to the specified value, the rest are set to zero)

This question (c initialization of a normal array with one default value) has more information

4
  • 1
    The empty initializer is invalid C (I believe it is valid C++).
    – pmg
    Commented Apr 25, 2012 at 8:15
  • @pmg or @codyschafer, can anyone point me to the standard where either of your arguments is stated please? initialization using ={ } appears to work in both gcc and g++, but I don't know whether its correct. Commented Oct 7, 2019 at 10:19
  • @GrimFandango: the C11 syntax for initialization does not allow empty initializer (same for C99). If you're using gcc, try gcc -std=c11 -pedantic ... otherwise gcc ... is the same as gcc -std=gnu11 ...
    – pmg
    Commented Oct 7, 2019 at 13:34
  • @GrimFandango: compare C11 6.7.9 (link in my previous comment) with C++14 8.5.1 (sorry, no link)
    – pmg
    Commented Oct 8, 2019 at 8:02
2

You may use

double myArray[3][12] = { 0 };

or

double myArray[3][12];
memset(myArray, 0, sizeof(double) * 3 * 12);
1

I think it will be

double myArray[3][12] = {0}
0

pmg's method works best as it works on the concept that if u initialise any array partially, rest of them get the default value of zero. else, u can declare the array as a global variable and when not initialised, the array elements will automatically be set to the default value (depending on compilers) zero.

Not the answer you're looking for? Browse other questions tagged or ask your own question.