I have a 3dimensional array, how do I initialize it to a default value without having 3 loops.
dummy[4][4][1024]
, how do I initialize all the elements to 12?
I have a 3dimensional array, how do I initialize it to a default value without having 3 loops.
dummy[4][4][1024]
, how do I initialize all the elements to 12?
Since the 3-d array is a contiguous block of memory, you can view it as a 1-d array
int i, *dummy2 = &dummy[0][0][0];
for(i = 0; i < 4*4*1024; ++i)
dummy2[i] = 12;
int i too :)int *dummy2 = (int *)dummy instead of &dummy[0][0][0]?long long. The version with the explicit cast still compiles but produces the wrong behavior.Come on guys - let's do it the simple way that always works:
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
for(int k = 0; k < 1024; k++)
{
dummy[i][j][k] = 12;
}
}
}
The default initialization to all zeros is just that:
unsigned dummy[4][4][1024] = { 0 };
If you want to initialize particular elements (and all others to zero) do this
unsigned dummy[4][4][1024] = { { { 5 }, { 0, 4 } } };
and if your compiler knows C99 use designated initializers
unsigned dummy[4][4][1024] = { [3] = { [2] = { [0] = 7 }, [1] = { [2] = 3, [1] = 4 } } };
and if you really insist to use all 12, just repeat the 12 16384 times :)