6

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!

3
  • 6
    You have to add commas between consecutive bracketed 'elements', too. Commented Nov 8, 2013 at 16:06
  • int arr[2][2][2]={{{1,2},{3,4}},{{5,6},{7,8}}}; Commented Nov 8, 2013 at 16:09
  • As Alexander said, commas after the "}" in the initialization. Commented Nov 8, 2013 at 16:09

5 Answers 5

7

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}
                         }
                      }
                    };
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks all of you! I do need to hardcode the array as the program will be doing some crazy searches through it.
This is not really "a , operator". This is just a comma, a syntactic element of an initializer. Is has no relation to the , operator.
4

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.

Comments

0

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

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.