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 ?

How many bytes are there in this array ?

What is the sizeof the array ?

How many single dimension arrays are present in this triple dimension array ?

How many double dimension arrays are present in this triple dimension array ?

What are the names of double dimension arrays in this triple dimension array ?

What are the names of single dimension arrays in this triple dimension array ?

How do you represent the first structure ?

How do you represent the last structure ?

How do you initialise the array at the time of declaration ?

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[0][0][0] is a Structure

a[1][0][0]

  • a[1][0][0] is a Structure

a[2][0][0]

  • a[2][0][0] is a Structure

&a[0][0][0]

  • &a[0][0][0] is address of a Structure

&a[1][0][0]

  • &a[1][0][0] is address of a Structure

&a[2][0][0]

  • &a[2][0][0] is address of a Structure

***a

  • ***a is Structure. Same as a[0][0][0]

*(*(*(a + 1) + 0) + 0)

  • *(*(*(a + 1) + 0) + 0) is a Structure. Same as a[1][0][0]

*(*(*(a + 2) + 0) + 0)

  • *(*(*(a + 2) + 0) + 0) is a Structure. Same as a[2][0][0]

a[0][0]

  • a[0][0] is a Single Dimension Array

a[1][0]

  • a[1][0] is a Single Dimension Array

a[2][0]

  • a[1][1] is a Single Dimension Array

&a[0][0]

  • &a[0][0] is address of a Single Dimension Array

&a[1][0]

  • &a[1][0] is address of a Single Dimension Array

&a[2][0]

  • &a[2][0] is address of a Single Dimension Array

**a

  • **a is a Single Dimension Array. Same as a[0][0]

*(*(a + 1) + 0)

  • *(*(a + 1) + 0) is a Single Dimension Array. Same as a[1][0]

*(*(a + 2) + 0)

  • *(*(a + 2) + 0) is a Single Dimension Array. Same as a[2][0]

a[0]

  • a[0] is a Double Dimension array

a[1]

  • a[1] is a Double Dimension array

a[2]

  • a[2] is a Double Dimension array

&a[0]

  • &a[0] is address of a Double Dimension array

&a[1]

  • &a[1] is address of a Double Dimension array

&a[2]

  • &a[2] is address of a Double Dimension array

*a

  • *a is a Double Dimension array

*(a + 1)

  • *(a + 1) is a Double Dimension array

*(a + 2)

  • *(a + 2) is a Double Dimension array

a

  • a is address of a Double Dimension array. Same as &a[0]

a + 1

  • a + 1 is address of a Double Dimension array. Same as &a[1]

a + 2

  • a + 2 is address of a Double Dimension array. Same as &a[2]

&a

  • &a is the address of Triple Dimension array

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

a[1][0][0]

*(*(*(a + 1) + 0) + 0)

a[2][0][0]

*(*(*(a + 2) + 0) + 0)

&a[0][0][0]

a[0][0]

&a[1][0][0]

a[1][0]

&a[2][0][0]

a[2][0]

***a

a[0][0][0]

*(*(*(a + 1) + 0) + 0)

a[1][0][0]

*(*(*(a + 2) + 0) + 0)

a[2][0][0]

a[0][0]

**a

a[1][0]

*(*(a + 1) + 0)

a[2][0]

*(*(a + 2) + 0)

&a[0][0]

a[0]

&a[1][0]

a[1]

&a[2][0]

a[2]

**a

a[0][0]

*(*(a + 1) + 0)

a[1][0]

*(*(a + 2) + 0)

a[2][0]

a[0]

*a

&a[0][0]

a[1]

*(a + 1)

&a[1][0]

a[2]

*(a + 2)

&a[2][0]

&a[0]

a + 0

a

&a[1]

a + 1

&a[2]

a + 2

*a

a[0]

&a[0][0]

*(a + 1)

a[1]

&a[1][0]

*(a + 2)

a[2]

&a[2][0]

a

&a[0]

a + 1

&a[1]

a + 2

&a[2]

&a

&a

Finding the type of an expression is easy. See below

Expression

Type

Description

a[0][0][0]

struct XYZ

  • is a Structure

a[1][0][0]

struct XYZ

  • is a Structure

a[2][0][0]

struct XYZ

  • is a Structure

&a[0][0][0]

struct XYZ

  • is a Structure

&a[1][0][0]

struct XYZ

  • is a Structure

&a[2][0][0]

struct XYZ

  • is a Structure

***a

struct XYZ

  • is a Structure

*(*(*(a + 1) + 0) + 0)

struct XYZ

  • is a Structure

*(*(*(a + 2) + 0) + 0)

struct XYZ

  • is a Structure

a[0][0]

struct XYZ *

  • a[0][0] is a Single Dimension Array

a[1][0]

struct XYZ *

  • a[1][0] is a Single Dimension Array

a[2][0]

struct XYZ *

  • a[2][0] is a Single Dimension Array

&a[0][0]

struct XYZ (*)[5]

  • &a[0][0] is address of a Single Dimension Array

&a[1][0]

struct XYZ (*)[5]

  • &a[1][0] is address of a Single Dimension Array

&a[2][0]

struct XYZ (*)[5]

  • &a[2][0] is address of a Single Dimension Array

**a

struct XYZ *

  • **a is a Single Dimension Array. Same as a[0][0]

*(*(a + 1) + 0)

struct XYZ *

  • *(*(a + 1) + 0) is a Single Dimension Array. Same as a[1][0]

*(*(a + 2) + 0)

struct XYZ *

  • *(*(a + 2) + 0) is a Single Dimension Array. Same as a[2][0]

a[0]

struct XYZ (*)[5]

  • a[0] is a Double Dimension structure array

  • a[0] is array of Single Dimension Arrays

  • Since every unit is a Single Dimension Array, a pointer to a single dimension array is needed

a[1]

struct XYZ (*)[5]

  • a[1] is a Double Dimension structure array

  • a[1] is array of Single Dimension Arrays

  • Since every unit is a Single Dimension Array, a pointer to a single dimension array is needed

a[2]

struct XYZ (*)[5]

  • a[2] is a Double Dimension structure array

  • a[2] is array of Single Dimension Arrays

  • Since every unit is a Single Dimension Array, a pointer to a single dimension array is needed

&a[0]

struct XYZ (*)[4][5]

  • a[0] is a Double Dimension structure array

  • Hence typeof(&a[0]) is struct XYZ (*)[4][5]

&a[1]

struct XYZ (*)[4][5]

  • a[1] is a Double Dimension structure array

  • Hence typeof(&a[1]) is struct XYZ (*)[4][5]

&a[2]

struct XYZ (*)[4][5]

  • a[2] is a Double Dimension structure array

  • Hence typeof(&a[2]) is struct XYZ (*)[4][5]

*a

struct XYZ (*)[5]

  • *a is a Double Dimension structure array

  • *a is array of Single Dimension Arrays

  • Since every unit is a Single Dimension Array, a pointer to a single dimension array is needed

*(a + 1)

struct XYZ (*)[5]

  • *(a + 1) is a Double Dimension structure array

  • *(a + 1) is array of Single Dimension Arrays

  • Since every unit is a Single Dimension Array, a pointer to a single dimension array is needed

*(a + 2)

struct XYZ (*)[5]

  • *(a + 2) is a Double Dimension structure array

  • *(a + 2) is array of Single Dimension Arrays

  • Since every unit is a Single Dimension Array, a pointer to a single dimension array is needed

a

struct XYZ (*)[4][5]

  • a is equal to &a[0]

  • Hence typeof(a) is struct XYZ (*)[4][5]

a + 1

struct XYZ (*)[4][5]

  • a + 1 is equal to &a[1]

  • Hence typeof(a + 1) is struct XYZ (*)[4][5]

a + 2

struct XYZ (*)[4][5]

  • a + 2 is equal to &a[2]

  • Hence typeof(a + 2) is struct XYZ (* )[4][5]

&a

struct XYZ (*)[3][4][5]

  • &a is address of complete array

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) {}

  • Call by Value

fun(a[1][0][0])

void fun(struct XYZ x) {}

  • Call by Value

fun(a[2][0][0])

void fun(struct XYZ x) {}

  • Call by Value

fun(&a[0][0][0])

void fun(struct XYZ *p) {}

  • Call by Reference

fun(&a[1][0][0])

void fun(struct XYZ *p) {}

  • Call by Reference

fun(&a[2][0][0])

void fun(struct XYZ *p) {}

  • Call by Reference

fun(a[0][0])

void fun(struct XYZ *p) {}

  • Call by Reference

fun(a[1][0])

void fun(struct XYZ *p) {}

  • Call by Reference

fun(a[2][0])

void fun(struct XYZ *p) {}

  • Call by Reference

fun(&a[0][0])

void fun(struct XYZ (*p)[5]) {}

  • Call by Reference

fun(&a[1][0])

void fun(struct XYZ (*p)[5]) {}

  • Call by Reference

fun(&a[2][0])

void fun(struct XYZ (*p)[5]) {}

  • Call by Reference

fun(**a)

void fun(struct XYZ *p) {}

  • Call by Reference

fun(*(*(a + 1) + 0))

void fun(struct XYZ *p) {}

  • Call by Reference

fun(*(*(a + 2) + 0))

void fun(struct XYZ *p) {}

  • Call by Reference

fun(a[0])

void fun(struct XYZ (*p)[5]) {}

  • Call by Reference

fun(a[1])

void fun(struct XYZ (*p)[5]) {}

  • Call by Reference

fun(a[2])

void fun(struct XYZ (*p)[5]) {}

  • Call by Reference

fun(&a[0])

void fun(struct XYZ (*p)[4][5]) {}

  • Call by Reference

fun(&a[1])

void fun(struct XYZ (*p)[4][5]) {}

  • Call by Reference

fun(&a[2])

void fun(struct XYZ (*p)[4][5]) {}

  • Call by Reference

fun(*a)

void fun(struct XYZ (*p)[5]) {}

  • Call by Reference

fun(*(a + 1))

void fun(struct XYZ (*p)[5]) {}

  • Call by Reference

fun(*(a + 2))

void fun(struct XYZ (*p)[5]) {}

  • Call by Reference

fun(a)

void fun(struct XYZ (*p)[4][5]) {}

  • Call by Reference

fun(a + 1)

void fun(struct XYZ (*p)[4][5]) {}

  • Call by Reference

fun(a + 2)

void fun(struct XYZ (*p)[4][5]) {}

  • Call by Reference

fun(&a)

void fun(struct XYZ (*p)[3][4][5]) {}

  • Call by Reference

Read more about function calls and conventions of Functions and Structure Triple Dimension Array