Reputation: 75
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
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
Reputation: 385
Remember C
always uses Row major order for memory allocation for statically declared arrays.
For Example:
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]
.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
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:
Inner braces denote matrix rows.
A comma must appear between each two adjacent matrix element initialization values (even if separated by a newline).
A comma should appear between adjacent pairs of inner braces; however, this comma might be optional in some standard compilers (?).
A trailing comma after the last element in each row is optional (if using inner braces).
A trailing comma after the last inner brace is optional.
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
Reputation: 8147
Each bracketed element can be viewed as one of the int
s 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
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