3d or multi-dimensional arrays

Well...This is the question that has been bugging me for a while. I have done a whole bunch of researches on this topic, but my effort is just in vain. I find no answer or solution to my problem. Here is the question. Since, in any C language, we could declare and initialize multi-dimensional array or just by putting the initializers of the array in the initializer list.

int billy [][][] = { 16, 2, 77, 40, 12071 };

By doing that, how do we know what kind of 3d array it is? Is it 1 x 1 x 3 or 3 x 3 x 1 and such?
test.c:1: error: elements of array `billy' have incomplete type
test.c:1: warning: array 'billy' assumed to have one element
test.c:1: error: storage size of `billy' isn't known
I know I just did it wrong...But I am just asking the concept question. Here it is:


int billy [][][] = { {{16, 2, 77}}, {{50,40, 12071}}};

How do we know what the indices are to access the 3d arrays? and how can we also tell what kind of array it is? 1 x 1 x 3 or something else?

When you declare an array you are required to declare it's size also. You can tell what kind of array it is by what you create it as and what the data is. For instance in the following code we know that the array is a 4X3 from looking at it's declaration.

int multiArray[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};

All levels but the first must have a declared size because the first is implied in it's declaration.

However, if you were to do it this way you would be required to state both sizes.

1
2
3
4
5
6
7
8
9
10
11
12
13
int multiArray[4][3];
multiArray[0][0] = 1;
multiArray[0][1] = 2;
multiArray[0][2] = 3;
multiArray[1][0] = 4;
multiArray[1][1] = 5;
multiArray[1][2] = 6;
multiArray[2][0] = 7;
multiArray[2][1] = 8;
multiArray[2][2] = 9;
multiArray[3][0] = 10;
multiArray[3][1] = 11;
multiArray[3][2] = 12;


If you want something that is not restrained by a declared size and instead can grow look into vectors.
Well...I know fairly well about two dimensional arrays. And I know 3D or even four as well. But what I don't understand or know is the initializer list of the 3D dimensional array. This has been bugging for a whole week. I am still trying to figure out. But I am at my wit's end. So come to ask questions and hope you guys could really help me out.

Aazor, by looking at the initializer list of the array of yours, I can clear tell or know what two indices are used to access the first value within the first set of the curly braces. So the initializer list, the first set of which is {1,2,3}. Since this is two dimensional array, I could tell that, just by looking at it, the value of 1 is accessed with two indices 0,0. And the value of 3 is accessed by the two indices of 0,2. Then the second set of the initializers within the curly brace {4,5,6}, I can tell that 5 is accessed with two indices of 1,1.

But when it comes to three dimensional array, I get stuck. {{{4,5,6}}, {{6,7,5}}, {{9,0,8}}}. I know each of the values within the sets of the curly braces are accessed with three indices since it is 3D dimensional array. But it is not as easy as just by looking at the values of the sets of curly braces of the two dimensional array and you could tell what the two indices are used. So if I would like to access the number or the value 4 within the three dimensional array, which is stated within the first set of the curly brace above, what indices are used? AND HOW DO I KNOW WHAT THE THREE INDICES ARE USED TO ACCESS THE VALUE 4,5 or 6?
Last edited on
Is this what you're asking?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int array[][][]={
	{
		{0,1,2},
		{3,4,5},
		{6,7,8}
	},
	{
		{9,10,11},
		{12,13,14},
		{15,16,17}
	},
	{
		{18,19,20},
		{21,22,23},
		{24,25,26}
	}
};
//array[0][0][0]==0 //1st table, 1st line, 1st column
//array[0][0][1]==1 //1st table, 1st line, 2nd column
//array[0][1][0]==3 //1st table, 2nd line, 1st column
//array[1][0][0]==9 //2nd table, 1st line, 1st column
//array[2][0][1]==19 //3rd table, 1st line, 2nd column 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int array[][][]={
	{
		{0,1,2},
		{3,4,5},
		{6,7,8}
	},
	{
		{9,10,11},
		{12,13,14},
		{15,16,17}
	},
	{
		{18,19,20},
		{21,22,23},
		{24,25,26}
	}
};


Yes it is but the question is if it is possible to do that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int array[][][]={
	{
		{0,1,2},
		{3,4,5},
		{6,7,8},
                {8,9,0},
                {9,0,9},
	},
	{
		{9,10,11},
		{12,13,14},
		{15,16,17}
	},
	{
		{18,19,20},
		{21,22,23},
		{24,25,26}
	}
};


1) Does it still count as three dimensional array?
2) What kind of three dimensional array is it of yours?
3 x 3 x 3? Or something?
Oh! No, you can't do that. All elements of each dimension must have the exact same size.
For example, you can have a rectangular array:
{{0,1,2},
{3,4,5},
{6,7,8}}
But you can't have this:
{{0,1,2},
{3,4,5,0},
{6,7,8}}
since that's not a rectangle anymore.

What you're trying to do is called an irregular matrix, and stack matrices don't allow it. It can be done, but you have to dynamically allocate the differently-sized sub-arrays yourself.

And yes, that 3D matrix is 3^3.
Well...I know if we wanna do something like the above which you said it is not possible is through the use of jagged array right?

{{0,1,2}
{3,4,5,6},
{6,7,8}}

well...the three dimensional array which is 3 x 3 x 3 is the above but what if we wanna create 3 x 2 x 3 array, what format does it look like? I'd like to get the heck out of it and understand 3d array better by just looking at it. Let me guess if I am correct:

so 3 x 2 x 3 should look like the one below right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int array[][][]={
	{
		{0,1,2},
		{3,4,5},
	},
	{
		{9,10,11},
		{12,13,14},
	},
	{
		{18,19,20},
		{21,22,23},
	}
};
Yeah, that's a 3x2x3, alright.

I should point out that explicitly using arrays with more than two dimensions is very, very uncommon. It's actually more common to create irregular arrays (e.g. an array of strings). More often, you'll create arrays of objects that themselves contain arrays, but you won't notice because of abstracted thinking.
Thanks a lot for helping and clarifying that. It is much better now.
Topic archived. No new replies allowed.