Just to reiterate what Jens has already said, {0}
works for any type. It is the "universal zero initializer" in C.
See C11 draft, 6.7.9 Initialization.
So, to initialize a 3D array either {0}
or {{{0}}}
can be used. Personally I'd use {0}
as it's easier to type and read and works for every type. That means, the following are all valid initializations:
int main(void)
{
int x = {0,};
int *p = {0,};
int *q = {0};
int arr[3][3][3][3] = {0};
}
More importantly, if you happen to have some unknown/opaque type, for example from a third-party libraries, then the only portable way
to initialize them is using {0}
. Any other way of zero-ing it (such as using memset()
or directly some_type_t state = 0;
) would require some internal knowledge of the type involved and risks being non-portable.