How could I simple initialize a multidimensional C-array with 0 elements like this:
int a[2][2] = { { 0, 0 }, {0, 0} }
This should work:
int a[2][2] = {0};
EDIT This trick may work for silencing the warning:
int a[2][2] = {{0}};
int a[2][2] = {}; should also work ?{} or {{0}}, only for {0}: warning: missing braces around initializer.gcc -Wall -Wextra -std=c99 -strict .... Can you point me at the relevant section of the C99 standard for this ?Use memset:
memset(a,0,sizeof a);
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.0 if your base type is a pointer or floating point type may not have the effect that you desire.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.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.
Use bzero to nullify it (faster than memset):
bzero(a, sizeof(a));
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.