Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare and initialize in a 4-dimensional array in C

I'm relatively new to programming, and I need to enter in a 4-dimensional array, and I can barely wrap my head around it. So, let's start with a simple 3-d array, 2 elements each like so:

int arr[2][2][2] = 
{
    {
        {1, 2}
        {3, 4}    //redline on "{"
    }
    {             //redline on "{"
        {5, 6}
        {7, 8}
    }
};

VS-2012 redlines the "{" before the "3", and says it's expecting a "}" instead. How would I be able to enter the array in a neat format? Having 4 dimensions will make it more complicated, and I need to be able to see the data clearly.

Also, I'm going to have a good number of zeroes in my array, in essense every arr[n][n] will be 0, so I'm wondering if I can make the initialization a little simpler.

my array will be of the type

int arr[7][7][15][2]

Or am I better off using struct instead?

Thanks in advance!

like image 898
user2809122 Avatar asked Nov 08 '13 16:11

user2809122


2 Answers

Each bracketed element can be viewed as one of the ints inside { 1, 2 } for example. Initializing with a list dictates that separate elements are enumerated with commas. The correct syntax thus will be with commas after each bracketed array ({ { 1, 2 }, { 3, 4 } }):

int arr[2][2][2] = 
{
    {
        {1, 2},
        {3, 4}
    },
    {            
        {5, 6},
        {7, 8}
    }
};

For a four-dimensional array you'd have to write a lot of 'hardcoding' code; you're probably better off writing a function that will fill it out with values for you.

like image 45
Alexander Avatar answered Nov 15 '22 07:11

Alexander


You're missing a , .

For a three dimensional array

 int arr[2][3][4] = { 
                       { 
                          {1, 2, 1, 2}, {1, 2, 1, 4}, {1, 2, 4, 4} 
                       },
                       {  
                          {1, 1, 2, 4}, {1, 2, 1, 4}, {1, 2, 1, 4} 
                       } 
                    };

or int arr[2][3][4] = {1,2,1,2,1,2,1,4,1,2,4,4,1,1,2,4,1,2,1,4,1,2,1,4}; 

For a four dimensional array

int arr[2][3][4][2] = {
                        {
                          {
                            {1,2},{1,2},{1,2},{4,2}
                          },
                          {
                            {2, 4},{1, 4},{1, 4},{1,2}
                          },
                          {
                            {2, 4},{1, 4},{1, 4},{1,8}
                          }
                       },
                       {
                         {
                           {1,2},{1,2},{1,2},{4,2}
                         },
                         {
                           {2, 4},{1, 4},{1, 4},{1,2}
                         },
                         {
                           {2, 4},{1, 4},{1, 4},{1,2}
                         }
                      }
                    };
like image 107
niko Avatar answered Nov 15 '22 07:11

niko