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

I'm relatively new to programming, and I need to go into a 4-dimensional array, and I can barely circle my head around it. So, let's start with a simple 3-dimensional array, 2 elements each like this:

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

VS-2012 redraws "{" before "3" and says that it expects "}". How can I enter an array in a neat format? Having 4 dimensions, this will complicate the situation, and I will need to clearly see the data.

In addition, I will have a large number of zeros in my array, in essence, each arr [n] [n] will be 0, so I wonder if I can make initialization a little easier.

my array will be of type

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

Or am I better off using struct instead?

Thanks in advance!

+6
source share
5 answers

You are missing,.

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} } } }; 
+5
source

Each element in square brackets can be considered as one of int inside { 1, 2 } , for example. List initialization dictates that individual items are listed with commas. Thus, the correct syntax will be with commas after each bracket array ( { { 1, 2 }, { 3, 4 } } ):

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

For a four-dimensional array, you need to write a lot of "hardcoding" code; you are probably better off writing a function that populates it with values โ€‹โ€‹for you.

+4
source

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

In two-stage C 1999 array declaration initiatives:

  • Inner braces denote the rows of the matrix.

  • A comma should appear between the initialization values โ€‹โ€‹of adjacent matrix elements (even if they are separated by a new line).

  • A comma should appear between adjacent pairs of internal braces; however, this comma may be optional in some standard compilers (?).

  • The trailing comma after the last element in each line is optional (if an inner bracket is used).

  • The trailing comma after the last parenthesis is optional.

  • An end semicolon after the last external reference is required.

(Can experts in C 1999 standard edit the list above to correct any false claims?)

0
source

Remember C always uses String order to allocate memory for statically declared arrays. For instance:

  • In the case of a 2-D array, say int A[3][2] , the memory allocation begins with the first element, that is, A[0][0] , then A[0][1] after that goes to the next line, but memory allocation will be contiguous, therefore, the next element in the order of memory location will be A[1][0] , and then A[1][1] -> A[2][0] -> A[2][1] .
  • Therefore, an easier way in your case of your initialization is to visualize it linearly in one-dimensional 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};

0
source

Four-dimensional array representation

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

Source: https://habr.com/ru/post/957689/


All Articles