Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to initialize an array?

Tags:

c

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?

like image 498
user1390048 Avatar asked Nov 29 '22 02:11

user1390048


2 Answers

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;
like image 108
Daniel Fischer Avatar answered Dec 15 '22 15:12

Daniel Fischer


Come on guys - let's do it the simple way that always works:


for(int i = 0; i &lt 4; i++)
{
  for(int j = 0; j &lt 4; j++)
  {
    for(int k = 0; k &lt 1024; k++)
    {
      dummy[i][j][k] = 12;
    }
  }
}

like image 45
Michael Dorgan Avatar answered Dec 15 '22 16:12

Michael Dorgan