2

How could I simple initialize a multidimensional C-array with 0 elements like this:

int a[2][2] = { { 0, 0 }, {0, 0} }
multiholle
  • 3,050
  • 8
  • 41
  • 60

6 Answers6

5

Use memset:

memset(a,0,sizeof a);
dcp
  • 54,410
  • 22
  • 144
  • 164
  • No, don't use `memset`. The catch all initializer `{0}` is guaranteed for all datatypes, even if "null" pointers or `0.0` aren't implemented with all bits set to 0. – Jens Gustedt Feb 08 '11 at 17:49
  • @Jens Gustedt - Yes, but your way *may* generate warnings depending on what compiler you are using and what compiler options. memset doesn't suffer from these issues (refer to the comments under Alexander Pogrebnyak's answer), so I think it is advantageous for that reason. – dcp Feb 09 '11 at 13:09
  • @dcp: because one compiler issues bogus warnings, you shouldn't give up portability. Setting all bits to `0` if your base type is a pointer or floating point type may not have the effect that you desire. – Jens Gustedt Feb 09 '11 at 13:29
  • @dcp code that doesn't work is not advantageous over code that does work but gets a warning. – Jim Balter Feb 09 '11 at 13:37
  • @Jens Gustedt - Good point, however, the OP's original requirement was "initialize a multidimensional C-array". We aren't trying to initialize pointer arrays here, so memset should be fine. – dcp Feb 09 '11 at 15:33
  • @Jim Balter - What exactly in the code I posted doesn't work? – dcp Feb 09 '11 at 15:34
  • @dcp, in C you can have multidimension arrays where the base type is a pointer type or `double`, no? – Jens Gustedt Feb 09 '11 at 16:10
  • @Jens Gustedt - Sure, I'm not arguing that. All I'm saying is that for the OP's original requirement, memset works fine. And I use memset all the time in the programming contests I do, and have never had any problems with it. The {0} technique you propose is fine as well, all I'm saying is that you're comment was "No, don't use memset", which I don't think is really fair, because my solution meets the OP's requirement just fine. – dcp Feb 09 '11 at 16:19
  • 1
    @dcp: I took the `int` in his code as an example. The question itself makes no reference to it. And in that sense it is better to have the compiler decide what is appropriate. In most cases it will do something equivalent to `memset`, but it is the compiler who knows better for the cases it isn't. – Jens Gustedt Feb 09 '11 at 16:37
  • @Jens The funny thing here is that the OP's code works just fine if a semicolon is added at the end, as I noted in my answer. All this talk of memset is totally off the mark -- the OP's question was about initializer syntax -- especially if the array is global: where is that memset supposed to occur? – Jim Balter Feb 10 '11 at 05:38
5

This should work:

int a[2][2] = {0};

EDIT This trick may work for silencing the warning:

int a[2][2] = {{0}};
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
  • May generate a warning though ? I believe `int a[2][2] = {};` should also work ? – Paul R Feb 08 '11 at 15:56
  • @Paul. I think empty brackets are not a legal syntax. On the other hand fattening 0 with extra pair of brackets works ( at least in gcc land ). – Alexander Pogrebnyak Feb 08 '11 at 16:05
  • @Alexander: with gcc I get no warnings for `{}` or `{{0}}`, only for `{0}`: *warning: missing braces around initializer*. – Paul R Feb 08 '11 at 16:09
  • @Jim: I'll take your word for it, but gcc doesn't complain even with `gcc -Wall -Wextra -std=c99 -strict ...`. Can you point me at the relevant section of the C99 standard for this ? – Paul R Feb 08 '11 at 16:27
  • I thought you were going to take my word for it? :-) 6.7.8: an initializer can be { initializer-list } and initializer-list can be designation_opt initializer, but initializer can't be empty. – Jim Balter Feb 08 '11 at 16:38
  • @Paul: you're missing `-pedantic` – Christoph Feb 08 '11 at 17:08
  • @Christoph: good catch - `-pedantic` gives me *ISO C forbids empty initializer braces* – Paul R Feb 08 '11 at 17:23
  • 1
    @Jim: thanks - well I *was* happy to take your word for it but at the same time I was also curious as to what it said about it in the standard. ;-) – Paul R Feb 08 '11 at 17:24
  • Yes the idea of gcc wanting to warn about this construct which is completely covered by the standard is anoying. "-Wno-missing-braces" or something like that switches this off. – Jens Gustedt Feb 08 '11 at 17:53
0
memset(a, 0, sizeof(a))
Thomas Edleson
  • 2,175
  • 1
  • 16
  • 19
Chaim Geretz
  • 826
  • 5
  • 23
0

Either use memset(), or make it static and just "lazy-initialize" to zero:

static int a[2][2];

Variables with static storage class are always initialized as if all all their fields were set to 0.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 2
    Making something static just to avoid explicit initialisation seems like bad advice - what about thread-safety, reentrancy, etc ? – Paul R Feb 08 '11 at 15:58
0

Use bzero to nullify it (faster than memset):

bzero(a, sizeof(a));
Benoit Thiery
  • 6,325
  • 4
  • 22
  • 28
  • bzero doesn't seem to be a standard c function though (http://www.google.com/search?hl=en&biw=1280&bih=622&q=bzero+nonstandard+c&aq=f&aqi=m1&aql=&oq=) – dcp Feb 08 '11 at 15:55
  • 2
    And memset typically drops through to bzero if you pass 0 as the second parameter anyway, so this is probably yet another example of premature optimisation... – Paul R Feb 08 '11 at 15:59
0

Simple: add a semicolon at the end:

int a[2][2] = { { 0, 0 }, {0, 0} };

And, if all the initializers really are zero and the array isn't local, you can omit the initializer altogether because 0 is the default:

int a[2][2];

People are suggesting calling memset or bzero, but those aren't relevant to the question as given.

Christoph
  • 164,997
  • 36
  • 182
  • 240
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • 1
    0 is only the default for globals and statics. Local variables have no default initialisation. – Paul R Feb 08 '11 at 16:00