0

Let's say I have a 9-dimensional array:

int arr[4][4][4][4][4][4][4][4][4];

I want to initialize every element to be 1.

I know I can initialize it using multiple loops:

for(int i1 = 0; i1 < 4; i ++) {
     ...// eight more similiar loops
}

But it seems ugly.

What is the best practice to do that? Maybe I can use macro?

nuk
  • 319
  • 1
  • 4
  • 7
  • Out of curiosity, for what do you need a 9d array? – this Apr 02 '14 at 03:17
  • For solving an ACM problem. – nuk Apr 02 '14 at 03:31
  • 1
    The only correct way would be to allocate a large array and then calculate index manually. Setting an array can be done in a single loop. – this Apr 02 '14 at 03:46
  • FWIW, if you wanted to initialize to 0 you could go with `int arr[4][4][4][4][4][4][4][4][4] = {{{{{{{{{0}}}}}}}}};`. I'm not aware of anything so simple(?) for an initialization value of 1, though. – Joshua Green Apr 02 '14 at 03:56
  • @JoshuaGreen You can drop the inner brackets; here is one way `int arr[4]...[4] = { #include "myfile.txt" }` – this Apr 02 '14 at 03:59
  • @self, you may be right, but doesn't that depend on what's in `myfile.txt`? – Joshua Green Apr 02 '14 at 04:02
  • @nuk Can I see that particulate ACM problem, if you are allowed to share it. – this Apr 02 '14 at 04:17
  • @self Actually it's a IOI'94 problem. [The Clocks](http://olympiads.win.tue.nl/ioi/ioi94/contest/day2prb1/problem.html) – nuk Apr 02 '14 at 19:21

4 Answers4

2

In C, multidimensional arrays are contiguous, so you can take advantage of this by initializing arr through a pointer.

    int* parr = (int*)arr;
    for (int i = 0; i < ((sizeof(arr)/sizeof(int)); ++i)
        parr[i] = 1;

Note that this won't work in situations where the array decomposes to a pointer (for example, if it was passed to a function as an argument).

ganz
  • 164
  • 4
  • I am curious why it won't work when the pointer is passed as an argument? – nuk Apr 02 '14 at 19:24
  • @nuk it's a little bit difficult to explain here, so a update my answer below – Jim Yang Apr 03 '14 at 02:11
  • Arrays decay into pointers when passed to functions. See this question: http://stackoverflow.com/questions/1461432/what-is-array-decaying. You may expect sizeof(arr) to give you the size of the array in bytes, but if arr is a pointer, sizeof(arr) will give you the size of the pointer in bytes. – ganz Apr 03 '14 at 02:31
1

write like this:

int *p = &arr[0][0][0][0][0][0][0][0][0];
for( int i = 0; i < 4*4*4*4*4*4*4*4*4; i++)
    *p++ = 1;

@nuk because arr is a type

int[4][4][4][4][4][4][4][4][4] // 9 of '[4]'

so

int arr[4][4][4][4][4][4][4][4][4];
sizeof(arr) = sizeof(int[4][4][4][4][4][4][4][4][4]);
            = sizeof(int)  *4*4*4*4*4*4*4*4*4;

but if you pass a pointer here,

int* arr;
sizeof(arr) = sizeof(int*)
            = sizeof(void*)
            = Usually the target file's BIT / 8
Jim Yang
  • 479
  • 2
  • 12
0

Maybe use a recursive function which is passed the number levels it needs to initialize.

void init(int * arr, int level, int size, int value)
{
     for(int i = 0; i < size; i++)
     {
          if(level == 0)
              arr[i] = value;
          else
              init(arr[i], level - 1, size, value);
     }
}
chasep255
  • 11,745
  • 8
  • 58
  • 115
0

If you had wanted to initialize to zero, a good old memset would have done it:

int arr[4][4][4][4][4][4][4][4][4];
memset(&arr, 0, sizeof(arr));

That said, however, and since you want to initialize to 1 anyway, I should say that it generally seems like an at least kinda poor idea to use 9D arrays in C. You'll always have to declare that draconian type anywhere you pass it to a function, and you'll never be able to use indirect indexing in any generic way. You're probably better off just declaring a 218 large int-array and calculate the index manually, instead. It's your context, of course, so it's not as if you couldn't have your reasons. :)

Dolda2000
  • 25,216
  • 4
  • 51
  • 92