Basics of Structure Triple Dimension Array
In this section, you are going to learn
What are the basic properties of a structure triple dimension array ?
What are the different expressions of triple dimension array ?
What are synonymous expresions of triple dimension array ?
How to find sizeof() of expressions of triple dimension array ?
struct XYZ array_name[Block][Row][Column];
Consider a structure triple dimension array
struct XYZ a[3][4][5];
Let us answer few basic questions in this array
How many structures can be stored in this array ?
See Answer
Number of Structures = 3 * 4 * 5 = 60
How many bytes are there in this array ?
See Answer
Number of Bytes = 3 * 4 * 5 * sizeof(struct XYZ) = 60 * 12 = 720 Bytes
What is the sizeof the array ?
See Answer
sizeof(a) = Number of Bytes = 3 * 4 * 5 * sizeof(struct XYZ) = 60 * 12 = 720 Bytes
How many single dimension arrays are present in this triple dimension array ?
See Answer
There are, 3 * 4 = 12 Single dimension arrays. Size of each single dimension array is 60 Bytes
How many double dimension arrays are present in this triple dimension array ?
See Answer
There are 3 Double dimension arrays. Size of each double dimension array is 4 * 5 * sizeof(struct XYZ) = 240 Bytes
What are the names of double dimension arrays in this triple dimension array ?
See Answer
a[0]
a[1]
a[2]
What are the names of single dimension arrays in this triple dimension array ?
See Answer
a[0][0]
a[0][1]
a[0][2]
a[0][3]
a[1][0]
a[1][1]
a[1][2]
a[1][3]
a[2][0]
a[2][1]
a[2][2]
a[2][3]
How do you represent the first structure ?
See Answer
a[0][0][0]
How do you represent the last structure ?
See Answer
a[2][3][4]
How do you initialise the array at the time of declaration ?
See Answer
struct XYZ
{
int x;
int y;
int z;
};
struct XYZ a[2][3][4] = {
{//a[0]
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
},
{//a[1]
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
},
};
Let us now explore basic examples of triple dimension array !
Step 1 : Define a Triple Dimension Array
struct XYZ a[2][3][4] = {
{//a[0]
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
},
{//a[1]
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
},
};
Step 2 : Access and Print individual structures
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 4; k++)
{
printf("a[%d][%d][%d].x = %d ", i, j, k, a[i][j][k].x);
printf("a[%d][%d][%d].y = %d ", i, j, k, a[i][j][k].y);
printf("a[%d][%d][%d].z = %d ", i, j, k, a[i][j][k].z);
printf("\n");
}
printf("\n");
}
printf("\n");
}
See the full program below
#include <stdio.h>
struct XYZ
{
int x;
int y;
int z;
};
int main(void)
{
struct XYZ a[2][3][4] = {
{//a[0]
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
},
{//a[1]
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
},
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 4; k++)
{
printf("a[%d][%d][%d].x = %d ", i, j, k, a[i][j][k].x);
printf("a[%d][%d][%d].y = %d ", i, j, k, a[i][j][k].y);
printf("a[%d][%d][%d].z = %d ", i, j, k, a[i][j][k].z);
printf("\n");
}
printf("\n");
}
printf("\n");
}
return 0;
}
Output is as below
a[0][0][0].x = 1 a[0][0][0].y = 1 a[0][0][0].z = 1
a[0][0][1].x = 2 a[0][0][1].y = 2 a[0][0][1].z = 2
a[0][0][2].x = 3 a[0][0][2].y = 3 a[0][0][2].z = 3
a[0][0][3].x = 4 a[0][0][3].y = 4 a[0][0][3].z = 4
a[0][1][0].x = 1 a[0][1][0].y = 1 a[0][1][0].z = 1
a[0][1][1].x = 2 a[0][1][1].y = 2 a[0][1][1].z = 2
a[0][1][2].x = 3 a[0][1][2].y = 3 a[0][1][2].z = 3
a[0][1][3].x = 4 a[0][1][3].y = 4 a[0][1][3].z = 4
a[0][2][0].x = 1 a[0][2][0].y = 1 a[0][2][0].z = 1
a[0][2][1].x = 2 a[0][2][1].y = 2 a[0][2][1].z = 2
a[0][2][2].x = 3 a[0][2][2].y = 3 a[0][2][2].z = 3
a[0][2][3].x = 4 a[0][2][3].y = 4 a[0][2][3].z = 4
a[1][0][0].x = 1 a[1][0][0].y = 1 a[1][0][0].z = 1
a[1][0][1].x = 2 a[1][0][1].y = 2 a[1][0][1].z = 2
a[1][0][2].x = 3 a[1][0][2].y = 3 a[1][0][2].z = 3
a[1][0][3].x = 4 a[1][0][3].y = 4 a[1][0][3].z = 4
a[1][1][0].x = 1 a[1][1][0].y = 1 a[1][1][0].z = 1
a[1][1][1].x = 2 a[1][1][1].y = 2 a[1][1][1].z = 2
a[1][1][2].x = 3 a[1][1][2].y = 3 a[1][1][2].z = 3
a[1][1][3].x = 4 a[1][1][3].y = 4 a[1][1][3].z = 4
a[1][2][0].x = 1 a[1][2][0].y = 1 a[1][2][0].z = 1
a[1][2][1].x = 2 a[1][2][1].y = 2 a[1][2][1].z = 2
a[1][2][2].x = 3 a[1][2][2].y = 3 a[1][2][2].z = 3
a[1][2][3].x = 4 a[1][2][3].y = 4 a[1][2][3].z = 4
Let us now explore Triple Dimension Array in depth !
Consider a Triple dimension array
struct XYZ a[3][4][5];
Below are the properties
Expressions
Synonyms
typeof(expression)
sizeof(expression)
fun(expression)
Know what are the possible expressions and how to properly call them !
Expression |
Description |
---|---|
a[0][0][0] |
|
a[1][0][0] |
|
a[2][0][0] |
|
&a[0][0][0] |
|
&a[1][0][0] |
|
&a[2][0][0] |
|
***a |
|
*(*(*(a + 1) + 0) + 0) |
|
*(*(*(a + 2) + 0) + 0) |
|
a[0][0] |
|
a[1][0] |
|
a[2][0] |
|
&a[0][0] |
|
&a[1][0] |
|
&a[2][0] |
|
**a |
|
*(*(a + 1) + 0) |
|
*(*(a + 2) + 0) |
|
a[0] |
|
a[1] |
|
a[2] |
|
&a[0] |
|
&a[1] |
|
&a[2] |
|
*a |
|
*(a + 1) |
|
*(a + 2) |
|
a |
|
a + 1 |
|
a + 2 |
|
&a |
|
Synonyms : Which can be used interchangeably !
Programmatically few expressions are one and the same ! Let us learn them
Expression |
Synonyms |
---|---|
a[0][0][0] |
|
a[1][0][0] |
|
a[2][0][0] |
|
&a[0][0][0] |
|
&a[1][0][0] |
|
&a[2][0][0] |
|
***a |
|
*(*(*(a + 1) + 0) + 0) |
|
*(*(*(a + 2) + 0) + 0) |
|
a[0][0] |
|
a[1][0] |
|
a[2][0] |
|
&a[0][0] |
|
&a[1][0] |
|
&a[2][0] |
|
**a |
|
*(*(a + 1) + 0) |
|
*(*(a + 2) + 0) |
|
a[0] |
|
a[1] |
|
a[2] |
|
&a[0] |
|
&a[1] |
|
&a[2] |
|
*a |
|
*(a + 1) |
|
*(a + 2) |
|
a |
|
a + 1 |
|
a + 2 |
|
&a |
|
Finding the type of an expression is easy. See below
Expression |
Type |
Description |
---|---|---|
a[0][0][0] |
struct XYZ |
|
a[1][0][0] |
struct XYZ |
|
a[2][0][0] |
struct XYZ |
|
&a[0][0][0] |
struct XYZ |
|
&a[1][0][0] |
struct XYZ |
|
&a[2][0][0] |
struct XYZ |
|
***a |
struct XYZ |
|
*(*(*(a + 1) + 0) + 0) |
struct XYZ |
|
*(*(*(a + 2) + 0) + 0) |
struct XYZ |
|
a[0][0] |
struct XYZ * |
|
a[1][0] |
struct XYZ * |
|
a[2][0] |
struct XYZ * |
|
&a[0][0] |
struct XYZ (*)[5] |
|
&a[1][0] |
struct XYZ (*)[5] |
|
&a[2][0] |
struct XYZ (*)[5] |
|
**a |
struct XYZ * |
|
*(*(a + 1) + 0) |
struct XYZ * |
|
*(*(a + 2) + 0) |
struct XYZ * |
|
a[0] |
struct XYZ (*)[5] |
|
a[1] |
struct XYZ (*)[5] |
|
a[2] |
struct XYZ (*)[5] |
|
&a[0] |
struct XYZ (*)[4][5] |
|
&a[1] |
struct XYZ (*)[4][5] |
|
&a[2] |
struct XYZ (*)[4][5] |
|
*a |
struct XYZ (*)[5] |
|
*(a + 1) |
struct XYZ (*)[5] |
|
*(a + 2) |
struct XYZ (*)[5] |
|
a |
struct XYZ (*)[4][5] |
|
a + 1 |
struct XYZ (*)[4][5] |
|
a + 2 |
struct XYZ (*)[4][5] |
|
&a |
struct XYZ (*)[3][4][5] |
|
sizeof(expression) |
size |
Description |
---|---|---|
sizeof(a[0][0][0]) |
12 |
a[0][0][0] is a structure |
sizeof(a[1][0][0]) |
12 |
a[1][0][0] is a structure |
sizeof(a[2][0][0]) |
12 |
a[2][0][0] is a structure |
sizeof(&a[0][0][0]) |
8 |
&a[0][0][0] is address / pointer |
sizeof(&a[1][0][0]) |
8 |
&a[1][0][0] is address / pointer |
sizeof(&a[2][0][0]) |
8 |
&a[2][0][0] is address / pointer |
sizeof(***a) |
12 |
***a is a structure |
sizeof(*(*(*(a + 1) + 0) + 0)) |
12 |
*(*(*(a + 1) + 0) + 0) is a structure |
sizeof(*(*(*(a + 2) + 0) + 0)) |
12 |
*(*(*(a + 2) + 0) + 0) is a structure |
sizeof(a[0][0]) |
60 |
a[0][0] is a Single Dimension Array |
sizeof(a[1][0]) |
60 |
a[1][0] is a Single Dimension Array |
sizeof(a[2][0]) |
60 |
a[2][0] is a Single Dimension Array |
sizeof(&a[0][0]) |
8 |
&a[0][0] is address / pointer |
sizeof(&a[1][0]) |
8 |
&a[1][0] is address / pointer |
sizeof(&a[2][0]) |
8 |
&a[2][0] is address / pointer |
sizeof(**a) |
60 |
**a is a Single Dimension Array |
sizeof(*(*(a + 1) + 0)) |
60 |
*(*(a + 1) + 0) is a Single Dimension Array |
sizeof(*(*(a + 2) + 0)) |
60 |
*(*(a + 2) + 0) is a Single Dimension Array |
sizeof(a[0]) |
240 |
a[0] is a Double Dimension Array |
sizeof(a[1]) |
240 |
a[1] is a Double Dimension Array |
sizeof(a[2]) |
240 |
a[2] is a Double Dimension Array |
sizeof(&a[0]) |
8 |
&a[0] is address / pointer |
sizeof(&a[1]) |
8 |
&a[1] is address / pointer |
sizeof(&a[2]) |
8 |
&a[2] is address / pointer |
sizeof(*a) |
240 |
*a is a Double Dimension Array |
sizeof(*(a + 1)) |
240 |
*(a + 1) is a Double Dimension Array |
sizeof(*(a + 2)) |
240 |
*(a + 2) is a Double Dimension Array |
sizeof(a) |
720 |
a is a triple dimension array |
sizeof(a + 1) |
8 |
a + 1 is address / pointer |
sizeof(a + 2) |
8 |
a + 2 is address / pointer |
sizeof(&a) |
8 |
&a is address / pointer |
See the full program below
#include <stdio.h>
int main(void)
{
struct XYZ a[3][4][5];
printf("sizeof(a[0][0][0]) = %d\n", (int) sizeof(a[0][0][0]) );
printf("sizeof(a[1][0][0]) = %d\n", (int) sizeof(a[1][0][0]) );
printf("sizeof(a[2][0][0]) = %d\n", (int) sizeof(a[2][0][0]) );
printf("sizeof(&a[0][0][0]) = %d\n", (int) sizeof(&a[0][0][0]) );
printf("sizeof(&a[1][0][0]) = %d\n", (int) sizeof(&a[1][0][0]) );
printf("sizeof(&a[2][0][0]) = %d\n", (int) sizeof(&a[2][0][0]) );
printf("sizeof(***a) = %d\n", (int) sizeof(***a) );
printf("sizeof(*(*(*(a + 1) + 0) + 0)) = %d\n", (int) sizeof(*(*(*(a + 1) + 0) + 0)) );
printf("sizeof(*(*(*(a + 2) + 0) + 0)) = %d\n", (int) sizeof(*(*(*(a + 2) + 0) + 0)) );
printf("sizeof(a[0][0]) = %d\n", (int) sizeof(a[0][0]) );
printf("sizeof(a[1][0]) = %d\n", (int) sizeof(a[1][0]) );
printf("sizeof(a[2][0]) = %d\n", (int) sizeof(a[2][0]) );
printf("sizeof(&a[0][0]) = %d\n", (int) sizeof(&a[0][0]) );
printf("sizeof(&a[1][0] = %d\n", (int) sizeof(&a[1][1]) );
printf("sizeof(&a[2][0]) = %d\n", (int) sizeof(&a[2][0]) );
printf("sizeof(**a) = %d\n", (int) sizeof(**a));
printf("sizeof(*(*(a + 1) + 0)) = %d\n", (int) sizeof(*(*(a + 1) + 0)) );
printf("sizeof(*(*(a + 2) + 0)) = %d\n", (int) sizeof(*(*(a + 2) + 0)) );
printf("sizeof(a[0]) = %d\n", (int) sizeof(a[0]) );
printf("sizeof(a[1]) = %d\n", (int) sizeof(a[1]) );
printf("sizeof(a[2]) = %d\n", (int) sizeof(a[2]) );
printf("sizeof(&a[0]) = %d\n", (int) sizeof(&a[0]) );
printf("sizeof(&a[1]) = %d\n", (int) sizeof(&a[1]) );
printf("sizeof(&a[2]) = %d\n", (int) sizeof(&a[2]) );
printf("sizeof(*a) = %d\n", (int) sizeof(*a) );
printf("sizeof(*(a + 1)) = %d\n", (int) sizeof(*(a + 1)) );
printf("sizeof(*(a + 2)) = %d\n", (int) sizeof(*(a + 2)) );
printf("sizeof(a) = %d\n", (int) sizeof(a) );
printf("sizeof(a + 1) = %d\n", (int) sizeof(a + 1) );
printf("sizeof(a + 2) = %d\n", (int) sizeof(a + 2) );
printf("sizeof(&a) = %d\n", (int) sizeof(&a) );
return 0;
}
Output is as below on 64 bit OS
sizeof(a[0][0][0]) = 12
sizeof(a[1][0][0]) = 12
sizeof(a[2][0][0]) = 12
sizeof(&a[0][0][0]) = 8
sizeof(&a[1][0][0]) = 8
sizeof(&a[2][0][0]) = 8
sizeof(***a) = 12
sizeof(*(*(*(a + 1) + 0) + 0)) = 12
sizeof(*(*(*(a + 2) + 0) + 0)) = 12
sizeof(a[0][0]) = 60
sizeof(a[1][0]) = 60
sizeof(a[2][0]) = 60
sizeof(&a[0][0]) = 8
sizeof(&a[1][0] = 8
sizeof(&a[2][0]) = 8
sizeof(**a) = 60
sizeof(*(*(a + 1) + 0)) = 60
sizeof(*(*(a + 2) + 0)) = 60
sizeof(a[0]) = 240
sizeof(a[1]) = 240
sizeof(a[2]) = 240
sizeof(&a[0]) = 8
sizeof(&a[1]) = 8
sizeof(&a[2]) = 8
sizeof(*a) = 240
sizeof(*(a + 1)) = 240
sizeof(*(a + 2)) = 240
sizeof(a) = 720
sizeof(a + 1) = 8
sizeof(a + 2) = 8
sizeof(&a) = 8
If fun(x)
is the function call, then fun(typeof(x))
is the prototype / definition
Function Call |
Function Definition |
Observations |
---|---|---|
fun(a[0][0][0]) |
void fun(struct XYZ x) {} |
|
fun(a[1][0][0]) |
void fun(struct XYZ x) {} |
|
fun(a[2][0][0]) |
void fun(struct XYZ x) {} |
|
fun(&a[0][0][0]) |
void fun(struct XYZ *p) {} |
|
fun(&a[1][0][0]) |
void fun(struct XYZ *p) {} |
|
fun(&a[2][0][0]) |
void fun(struct XYZ *p) {} |
|
fun(a[0][0]) |
void fun(struct XYZ *p) {} |
|
fun(a[1][0]) |
void fun(struct XYZ *p) {} |
|
fun(a[2][0]) |
void fun(struct XYZ *p) {} |
|
fun(&a[0][0]) |
void fun(struct XYZ (*p)[5]) {} |
|
fun(&a[1][0]) |
void fun(struct XYZ (*p)[5]) {} |
|
fun(&a[2][0]) |
void fun(struct XYZ (*p)[5]) {} |
|
fun(**a) |
void fun(struct XYZ *p) {} |
|
fun(*(*(a + 1) + 0)) |
void fun(struct XYZ *p) {} |
|
fun(*(*(a + 2) + 0)) |
void fun(struct XYZ *p) {} |
|
fun(a[0]) |
void fun(struct XYZ (*p)[5]) {} |
|
fun(a[1]) |
void fun(struct XYZ (*p)[5]) {} |
|
fun(a[2]) |
void fun(struct XYZ (*p)[5]) {} |
|
fun(&a[0]) |
void fun(struct XYZ (*p)[4][5]) {} |
|
fun(&a[1]) |
void fun(struct XYZ (*p)[4][5]) {} |
|
fun(&a[2]) |
void fun(struct XYZ (*p)[4][5]) {} |
|
fun(*a) |
void fun(struct XYZ (*p)[5]) {} |
|
fun(*(a + 1)) |
void fun(struct XYZ (*p)[5]) {} |
|
fun(*(a + 2)) |
void fun(struct XYZ (*p)[5]) {} |
|
fun(a) |
void fun(struct XYZ (*p)[4][5]) {} |
|
fun(a + 1) |
void fun(struct XYZ (*p)[4][5]) {} |
|
fun(a + 2) |
void fun(struct XYZ (*p)[4][5]) {} |
|
fun(&a) |
void fun(struct XYZ (*p)[3][4][5]) {} |
|
Read more about function calls and conventions of Functions and Structure Triple Dimension Array
Other topics of character and functions
Current Module
Previous Module
Next Module
Other Modules