user2809122
user2809122

Reputation: 75

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!

Upvotes: 6

Views: 24741

Answers (5)

Barla Chaitanya
Barla Chaitanya

Reputation: 1

A representation of a four dimensional array

int arr[2][2][2][2] = 
{
    { { {1, 2}, {3, 4} }, { {5, 6}, {7, 8} } }, 
    { { {9,10}, {11, 12} }, { {13, 14}, {15, 16} } }
};

Upvotes: -1

user3496912
user3496912

Reputation: 385

Remember C always uses Row major order for memory allocation for statically declared arrays. For Example:

  • In the case of 2-D array, say int A[3][2] the memory allocation starts from the first element i.e A[0][0] then A[0][1] after that jumps to the next row but the memory allocation would be contiguous, hence next element in the order of memory layout would be A[1][0] then A[1][1] -> A[2][0] -> A[2][1].
  • Hence the simpler way in your case of your initialization would be visualize it linearly in the 1-D form and write it down.

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

Upvotes: 0

anon7
anon7

Reputation: 1

Inside the outer braces, the inner braces { } denote matrix rows. But which commas are required? And which commas are optional?

In C 1999 two-dimensional array declaration initializations:

  1. Inner braces denote matrix rows.

  2. A comma must appear between each two adjacent matrix element initialization values (even if separated by a newline).

  3. A comma should appear between adjacent pairs of inner braces; however, this comma might be optional in some standard compilers (?).

  4. A trailing comma after the last element in each row is optional (if using inner braces).

  5. A trailing comma after the last inner brace is optional.

  6. A trailing semicolon after the last outer brace is required.

(Could experts in standard C 1999 please edit the above list to correct any false statements?)

Upvotes: 0

Alexander
Alexander

Reputation: 8147

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.

Upvotes: 4

niko
niko

Reputation: 9393

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

Upvotes: 7

Related Questions