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;
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With