2

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

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

6 Answers 6

5

This should work:

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

EDIT This trick may work for silencing the warning:

int a[2][2] = {{0}};
Sign up to request clarification or add additional context in comments.

9 Comments

May generate a warning though ? I believe int a[2][2] = {}; should also work ?
@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: with gcc I get no warnings for {} or {{0}}, only for {0}: warning: missing braces around initializer.
@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 ?
@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. ;-)
|
5

Use memset:

memset(a,0,sizeof a);

10 Comments

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 - 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: 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.
@dcp code that doesn't work is not advantageous over code that does work but gets a warning.
@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.
|
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.

1 Comment

Making something static just to avoid explicit initialisation seems like bad advice - what about thread-safety, reentrancy, etc ?
0

Use bzero to nullify it (faster than memset):

bzero(a, sizeof(a));

2 Comments

bzero doesn't seem to be a standard c function though (google.com/…)
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...
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.

1 Comment

0 is only the default for globals and statics. Local variables have no default initialisation.
0
memset(a, 0, sizeof(a))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.