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?

8
  • Out of curiosity, for what do you need a 9d array? Commented Apr 2, 2014 at 3:17
  • For solving an ACM problem. Commented Apr 2, 2014 at 3: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. Commented Apr 2, 2014 at 3: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. Commented Apr 2, 2014 at 3:56
  • @JoshuaGreen You can drop the inner brackets; here is one way int arr[4]...[4] = { #include "myfile.txt" } Commented Apr 2, 2014 at 3:59

4 Answers 4

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).

Sign up to request clarification or add additional context in comments.

3 Comments

I am curious why it won't work when the pointer is passed as an argument?
@nuk it's a little bit difficult to explain here, so a update my answer below
Arrays decay into pointers when passed to functions. See this question: 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.
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

Comments

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);
     }
}

2 Comments

I suppose the single loops in the other responses would be more efficient but I think my solution is less language dependent.
Maybe init(&arr[i*width(level, size)], level - 1, size, value);?
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. :)

4 Comments

That works great for an initialization value of 0, but there isn't anything so simple for an initialization value of 1.
Oh, right. I skipped over that part of the question. Oh well, I'll let it stand for the more common case. At least the second part is still valid.
I'd be careful here. This answer is a bit deceptive since changing that 0 to a 1 simply won't work.
Sure enough. I've clarified the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.