Malloc struct Triple pointer

  • In this section, you are going to learn

How to allocate memory using malloc ?

How to allocate memory using calloc ?

How to allocate memory using realloc ?

  • Key point to remember

    • Triple pointer points to array of double pointers

    • Double pointer points to array of single pointers

    • Single pointer points to array of structures

  • Example is arr[2][3][4]

  • Now let us take a look at real time examples

  • Step 1 : Include stdlib.h

#include <stdlib.h>
  • Step 2 : Define Triple Pointer

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC ***ptr;
  • Step 3.1 : ptr is a triple pointer and points to array of double pointers

ptr = malloc(sizeof(struct ABC **));
  • Step 3.2 : *ptr is a double pointer and points to array of single pointers

*ptr = malloc(sizeof(struct ABC *));
  • Step 3.3 : **ptr is a single pointer and points to array of structures

**ptr = malloc(sizeof(struct ABC));
  • Step 4 : Assign a value to structure

(***ptr).a = 65;

OR

(**ptr)[0].a = 65;

OR

(*ptr)[0][0].a = 65;

OR

ptr[0][0][0].a = 65;

OR

ptr[0][0]->a = 65;

OR

(*ptr[0])->a = 65;
  • Step 5 : Print the value of structure

printf("(***ptr).a = %d\n", (***ptr).a);

OR

printf("(**ptr)[0].a = %d\n", (**ptr)[0].a);

OR

printf("(*ptr)[0][0].a = %d\n", (*ptr)[0][0].a);

OR

printf("ptr[0][0][0].a = %d\n",  ptr[0][0][0].a);

OR

printf("ptr[0][0]->a = %d\n", ptr[0][0]->a);

OR

printf("(*ptr[0])->a = %d\n", (*ptr[0])->a);
  • Step 6.1 : Free array of structures

free(**ptr);
  • Step 6.2 : Free array of single pointers

free(*ptr);
  • Step 6.3 : Free array of double pointers

free(ptr);
  • See the full program below

 1#include <stdio.h>
 2
 3// Step 1 : Include stdlib.h
 4#include <stdlib.h>
 5
 6struct ABC {
 7        int a;
 8        int b;
 9        int c;
10};
11
12int main(void)
13{
14        // Step 2 : Define Triple Pointer
15        struct ABC ***ptr;
16
17        // Step 3.1 : ptr is a triple pointer and points to array of double pointers
18        ptr = malloc(sizeof(struct ABC **));
19
20        // Step 3.2 : *ptr is a double pointer and points to array of single pointers
21        *ptr = malloc(sizeof(struct ABC *));
22
23        // Step 3.3 : **ptr is a single pointer and points to array of structures
24        **ptr = malloc(sizeof(struct ABC));
25
26        // Step 4 : Assign a value to structure
27        (***ptr).a = 65;
28        (**ptr)[0].a = 65;
29        (*ptr)[0][0].a = 65;
30        ptr[0][0][0].a = 65;
31        ptr[0][0]->a = 65;
32        (*ptr[0])->a = 65;
33
34        // Step 5 : Print the value of structure
35        printf("(***ptr).a = %d\n", (***ptr).a);
36        printf("(**ptr)[0].a = %d\n", (**ptr)[0].a);
37        printf("(*ptr)[0][0].a = %d\n", (*ptr)[0][0].a);
38        printf("ptr[0][0][0].a = %d\n",  ptr[0][0][0].a);
39        printf("ptr[0][0]->a = %d\n", ptr[0][0]->a);
40        printf("(*ptr[0])->a = %d\n", (*ptr[0])->a);
41
42        // Step 6.1 : Free array of structures
43        free(**ptr);
44
45        // Step 6.2 : Free array of single pointers
46        free(*ptr);
47
48       // Step 6.3 : Free array of double pointers
49        free(ptr);
50
51        return 0;
52}

Statement

Description

ptr

  • ptr is a triple pointer

*ptr

  • *ptr is a double pointer

**ptr

  • **ptr is a single pointer

***ptr

  • ***ptr is a structure

sizeof(ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(*ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence *ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(**ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence **ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(***ptr)

  • 12 Bytes

  • Fully dereferenced

  • Hence ***ptr is a structure

  • sizeof(structure) is 12 Bytes in this case

ptr = malloc( sizeof(struct ABC ** ) )

  • ptr points to array of double pointers

*ptr = malloc( sizeof(struct ABC * ) )

  • *ptr points to array of single pointers

**ptr = malloc( sizeof(struct ABC ) )

  • **ptr points to array of structures

(***ptr).a

  • Method 1 : Member Access

(**ptr)[0].a

  • Method 2 : Member Access

(*ptr)[0][0].a

  • Method 3 : Member Access

ptr[0][0][0].a

  • Method 4 : Member Access

ptr[0][0]->a

  • Method 5 : Member Access

(*ptr[0])->a

  • Method 6 : Member Access

  • Step 1 : Include stdlib.h

#include <stdlib.h>
  • Step 2 : Define Triple Pointer

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC ***ptr;
  • Step 3.1 : ptr is a triple pointer and points to array of double pointers

ptr = malloc(sizeof(struct ABC **));
  • Step 3.2 : *ptr is a double pointer and points to array of single pointers

*ptr = malloc(sizeof(struct ABC *));
  • Step 3.3 : **ptr is a single pointer and points to array of 10 structure objects

**ptr = malloc(10 * sizeof(struct ABC));
  • Step 4 : Assign a value to structure

for (int i = 0; i < 10; i++)
{
        ptr[0][0][i].a = ++val;
}

OR

for (int i = 0; i < 10; i++)
{
        (**ptr)[i].a = ++val;
}

OR

for (int i = 0; i < 10; i++)
{
        (*ptr)[0][i].a = ++val;
}

OR

for (int i = 0; i < 10; i++)
{
        ((**ptr) + i)->a = ++val;
}

OR

for (int i = 0; i < 10; i++)
{
        (ptr[0][0] + i)->a = ++val;
}

OR

for (int i = 0; i < 10; i++)
{
        ((*ptr)[0] + i)->a = ++val;
}
  • Step 5 : Print the value of structure

for (int i = 0; i < 10; i++)
{
        printf("ptr[0][0][%d].a = %d\n", i, ptr[0][0][i].a);
}

OR

for (int i = 0; i < 10; i++)
{
        printf("(**ptr)[%d].a = %d\n", i, (**ptr)[i].a);
}

OR

for (int i = 0; i < 10; i++)
{
        printf("(*ptr)[0][%d].a = %d\n", i, (*ptr)[0][i].a);
}

OR

for (int i = 0; i < 10; i++)
{
        printf("((**ptr) + %d)->a = %d\n", i, ((**ptr) + i)->a);
}

OR

for (int i = 0; i < 10; i++)
{
        printf("(ptr[0][0] + %d)->a = %d\n", i, (ptr[0][0] + i)->a);
}

OR

for (int i = 0; i < 10; i++)
{
        printf("((*ptr)[0] + %d)->a = %d\n", i, ((*ptr)[0] + i)->a);
}
  • Step 6.1 : Free array of structures

free(**ptr);
  • Step 6.2 : Free array of single pointers

free(*ptr);
  • Step 6.3 : Free array of double pointers

free(ptr);
  • See the full program below

 1#include <stdio.h>
 2
 3// Step 1 : Include stdlib.h
 4#include <stdlib.h>
 5
 6struct ABC {
 7        int a;
 8        int b;
 9        int c;
10};
11
12int main(void)
13{
14        // Step 2 : Define Triple Pointer
15        struct ABC ***ptr;
16        int val = 0;
17
18        // Step 3.1 : ptr is a triple pointer and points to array of double pointers
19        ptr = malloc(sizeof(struct ABC **));
20
21        // Step 3.2 : *ptr is a double pointer and points to array of single pointers
22        *ptr = malloc(sizeof(struct ABC *));
23
24        // Step 3.3 : **ptr is a single pointer and points to array of 10 structure objects
25        **ptr = malloc(10 * sizeof(struct ABC));
26
27        // Step 4 : Assign a value to structure
28        for (int i = 0; i < 10; i++)
29        {
30                ptr[0][0][i].a = ++val;
31        }
32
33        for (int i = 0; i < 10; i++)
34        {
35                (**ptr)[i].a = ++val;
36        }
37
38        for (int i = 0; i < 10; i++)
39        {
40                (*ptr)[0][i].a = ++val;
41        }
42
43        for (int i = 0; i < 10; i++)
44        {
45                ((**ptr) + i)->a = ++val;
46        }
47
48        for (int i = 0; i < 10; i++)
49        {
50                (ptr[0][0] + i)->a = ++val;
51        }
52
53        for (int i = 0; i < 10; i++)
54        {
55                ((*ptr)[0] + i)->a = ++val;
56        }
57
58        // Step 5 : Print the value of structure
59        for (int i = 0; i < 10; i++)
60        {
61                printf("ptr[0][0][%d].a = %d\n", i, ptr[0][0][i].a);
62        }
63
64        for (int i = 0; i < 10; i++)
65        {
66                printf("(**ptr)[%d].a = %d\n", i, (**ptr)[i].a);
67        }
68
69        for (int i = 0; i < 10; i++)
70        {
71                printf("(*ptr)[0][%d].a = %d\n", i, (*ptr)[0][i].a);
72        }
73
74        for (int i = 0; i < 10; i++)
75        {
76                printf("((**ptr) + %d)->a = %d\n", i, ((**ptr) + i)->a);
77        }
78
79        for (int i = 0; i < 10; i++)
80        {
81                printf("(ptr[0][0] + %d)->a = %d\n", i, (ptr[0][0] + i)->a);
82        }
83
84        for (int i = 0; i < 10; i++)
85        {
86                printf("((*ptr)[0] + %d)->a = %d\n", i, ((*ptr)[0] + i)->a);
87        }
88
89        // Step 6.1 : Free array of structures
90        free(**ptr);
91
92        // Step 6.2 : Free array of single pointers
93        free(*ptr);
94
95        // Step 6.3 : Free array of double pointers
96        free(ptr);
97
98        return 0;
99}

Statement

Description

ptr

  • ptr is a triple pointer

*ptr

  • *ptr is a double pointer

**ptr

  • **ptr is a single pointer

***ptr

  • ***ptr is a structure

sizeof(ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(*ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence *ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(**ptr)

  • 8 Bytes

  • Not fully dereferenced

  • Hence **ptr is a pointer

  • sizeof(pointer) is 8 Bytes always

sizeof(***ptr)

  • 12 Bytes

  • Fully dereferenced

  • Hence ***ptr is a structure

  • sizeof(structure) is 12 Bytes in this case

ptr = malloc( sizeof(struct ABC ** ) )

  • ptr points to array of double pointers

*ptr = malloc( sizeof(struct ABC * ) )

  • *ptr points to array of single pointers

**ptr = malloc( 10 * sizeof(struct ABC) )

  • **ptr points to array of 10 structure objects

ptr[ 0 ][ 0 ][ i ].a

  • Method 1 : Member Access

(**ptr)[ i ].a

  • Method 2 : Member Access

(*ptr)[ 0 ][ i ].a

  • Method 3 : Member Access

((**ptr) + i)->a

  • Method 4 : Member Access

(ptr[ 0 ][ 0 ] + i)->a

  • Method 5 : Member Access

((*ptr)[ 0 ] + i)->a

  • Method 6 : Member Access

  • Step 1 : Include stdlib.h

#include <stdlib.h>
  • Step 2 : Define Triple Pointer

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC ***ptr;
  • Step 3.1 : Create array of double pointers. In this case, create one double pointer ptr[0]

ptr = malloc(sizeof(struct ABC **));
  • Step 3.2 : Create array of single pointers from ptr[0][0]ptr[0][9]

*ptr = malloc(10 * sizeof(struct ABC *));

OR

ptr[0] = malloc(10 * sizeof(struct ABC *));
  • Step 3.3 : Create array of structures from ptr[0][0][0] to ptr[0][9][9]

for (int i = 0; i < 10; i++) {
        (*ptr)[i] = malloc(10 * sizeof(struct ABC));
}

OR

for (int i = 0; i < 10; i++) {
        ptr[0][i] = malloc(10 * sizeof(struct ABC));
}
  • Step 4 : Assing values to structure members from ptr[0][0][0] to ptr[0][9][9]

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                ptr[0][i][j].a = ++val;
        }
}
  • Step 5 : Print values of structure members from ptr[0][0][0] to ptr[0][9][9]

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                printf("ptr[0][i][j].a = %d\n", ptr[0][i][j].a);
        }
}
  • Step 6.1 : Free array of structures pointed by all single pointers

for (int i = 0; i < 10; i++) {
        free((*ptr)[i]);
}
  • Step 6.2 : Free array of single pointers

free(*ptr);
  • Step 6.3 : Free array of double pointers

free(ptr);
  • See the full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4// Step 1 : Include stdlib.h
 5#include <stdlib.h>
 6
 7struct ABC {
 8        int a;
 9        int b;
10        int c;
11};
12
13int main(void)
14{
15        // Step 2 : Define Triple Pointer
16        struct ABC ***ptr;
17        int val = 0;
18
19        // Step 3.1 : Create array of double pointers. In this case, create one double pointer ``ptr[0]``
20        ptr = malloc(sizeof(struct ABC **));
21
22        // Step 3.2 : Create array of single pointers from ``ptr[0][0]`` ... ``ptr[0][9]``
23        *ptr = malloc(10 * sizeof(struct ABC *));
24
25        // Step 3.3 : Create array of structures from ``ptr[0][0][0]`` to ``ptr[0][9][9]``
26        for (int i = 0; i < 10; i++) {
27                (*ptr)[i] = malloc(10 * sizeof(struct ABC));
28        }
29
30        // Step 4 : Assing values to structure members from ``ptr[0][0][0]`` to ``ptr[0][9][9]``
31
32        for (int i = 0; i < 10; i++) {
33                for (int j = 0; j < 10; j++) {
34                        ptr[0][i][j].a = ++val;
35                }
36        }
37
38        // Step 5 : Print values of structure members from ``ptr[0][0][0]`` to ``ptr[0][9][9]``
39
40        for (int i = 0; i < 10; i++) {
41                for (int j = 0; j < 10; j++) {
42                        printf("ptr[0][%d][%d].a = %d\n", i, j, ptr[0][i][j].a);
43                }
44        }
45
46        // Step 6.1 : Free array of structures pointed by all single pointers
47        for (int i = 0; i < 10; i++) {
48                free((*ptr)[i]);
49        }
50
51        // Step 6.2 : Free array of single pointers
52        free(*ptr);
53
54        // Step 6.3 : Free array of double pointers
55        free(ptr);
56
57        return 0;
58}
  • Step 1 : Include stdlib.h

#include <stdlib.h>
  • Step 2 : Define Triple Pointer

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC ***ptr;
  • Step 3.1 : Create array of double pointers. In this case, create ten double pointers from ptr[0] to ptr[9]

ptr = malloc(10 * sizeof(struct ABC **));
  • Step 3.2 : Create array of single pointers from ptr[0][0] to ptr[9][9]

for (int i = 0; i < 10; i++)
{
        ptr[i] = malloc(10 * sizeof(struct ABC *));
}
  • Step 3.3 : Create array of structures from ptr[0][0][0] to ptr[9][9][9]

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                ptr[i][j] = malloc(10 * sizeof(struct ABC));
        }
}
  • Step 4 : Assing values to structure members from ptr[0][0][0] to ptr[9][9][9]

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                for (int k = 0; k < 10; k++) {
                        ptr[i][j][k].a = ++val;
                }
        }
}
  • Step 5 : Print values of structure members from ptr[0][0][0] to ptr[0][9][9]

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                for (int k = 0; k < 10; k++) {
                        printf("ptr[%d][%d][%d].a = %d\n", i, j, k, ptr[i][j][k].a);
                }
        }
}
  • Step 6.1 : Free array of structures from ptr[0][0][0] to ptr[9][9][9]

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                free(ptr[i][j]);
        }
}
  • Step 6.2 : Free array of single pointers from ptr[0][0] to ptr[9][9]

for (int i = 0; i < 10; i++)
{
        free(ptr[i]);
}
  • Step 6.3 : Free array of double pointers

free(ptr);
  • See the full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4// Step 1 : Include stdlib.h
 5#include <stdlib.h>
 6
 7struct ABC {
 8        int a;
 9        int b;
10        int c;
11};
12
13int main(void)
14{
15        // Step 2 : Define Triple Pointer
16        struct ABC ***ptr;
17        int val = 0;
18
19        // Step 3.1 : Create array of double pointers. In this case, create ten double pointers from ``ptr[0]`` to ``ptr[9]``
20        ptr = malloc(10 * sizeof(struct ABC **));
21
22        // Step 3.2 : Create array of single pointers from ``ptr[0][0]`` ... ``ptr[9][9]``
23        for (int i = 0; i < 10; i++)
24        {
25                ptr[i] = malloc(10 * sizeof(struct ABC *));
26        }
27
28        // Step 3.3 : Create array of structures from ``ptr[0][0][0]`` to ``ptr[9][9][9]``
29        for (int i = 0; i < 10; i++) {
30                for (int j = 0; j < 10; j++) {
31                        ptr[i][j] = malloc(10 * sizeof(struct ABC));
32                }
33        }
34
35        // Step 4 : Assing values to structure members from ``ptr[0][0][0]`` to ``ptr[9][9][9]``
36        for (int i = 0; i < 10; i++) {
37                for (int j = 0; j < 10; j++) {
38                        for (int k = 0; k < 10; k++) {
39                                ptr[i][j][k].a = ++val;
40                        }
41                }
42        }
43
44        // Step 5 : Print values of structure members from ``ptr[0][0][0]`` to ``ptr[0][9][9]``
45        for (int i = 0; i < 10; i++) {
46                for (int j = 0; j < 10; j++) {
47                        for (int k = 0; k < 10; k++) {
48                                printf("ptr[%d][%d][%d].a = %d\n", i, j, k, ptr[i][j][k].a);
49                        }
50                }
51        }
52
53        // Step 6.1 : Free array of structures from ``ptr[0][0][0]`` to ``ptr[9][9][9]``
54        for (int i = 0; i < 10; i++) {
55                for (int j = 0; j < 10; j++) {
56                        free(ptr[i][j]);
57                }
58        }
59
60        // Step 6.2 : Free array of single pointers from ``ptr[0][0]`` ... ``ptr[9][9]``
61        for (int i = 0; i < 10; i++)
62        {
63                free(ptr[i]);
64        }
65
66        // Step 6.3 : Free array of double pointers. In this case, free ten double pointers from ``ptr[0]`` to ``ptr[9]``
67        free(ptr);
68
69        return 0;
70}
  • Step 1 : Include stdlib.h

#include <stdlib.h>
  • Step 2 : Define an allocation function

int get_memory(struct ABC ****fp)
{
        return 0;
}
  • Step 3.1 : Create array of double pointers from (*fp)[0](*fp)[9]

int get_memory(struct ABC ****fp)
{
        // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9]
        *fp = malloc(10 * sizeof(struct ABC **));

        return 0;
}
  • Step 3.2 : Create array of single pointers for every double pointer

int get_memory(struct ABC ****fp)
{
        // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9]
        *fp = malloc(10 * sizeof(struct ABC **));

        // Step 3.2 : Create array of single pointers for every double pointer
        for (int i = 0; i < 10; i++) {
                (*fp)[i] = malloc(10 * sizeof(struct ABC *));
        }

        return 0;
}
  • Step 3.3 : Create array of structures for every single pointer from (*fp)[0][9](*fp)[9][9]

int get_memory(struct ABC ****fp)
{
        // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9]
        *fp = malloc(10 * sizeof(struct ABC **));

        // Step 3.2 : Create array of single pointers for every double pointer
        for (int i = 0; i < 10; i++) {
                (*fp)[i] = malloc(10 * sizeof(struct ABC *));
        }

        // Step 3.3 : Create array of structures for every single pointer from (*fp)[0][9] ... (*fp)[9][9]
        for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                        (*fp)[i][j] = malloc(10 * sizeof(struct ABC));
                }
        }

        return 0;
}
  • Step 4 : Define Triple Pointer in caller

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC ***tp;
  • Step 5 : Pass Triple pointer as reference

get_memory(&tp);
  • Step 6 : Assing values to structure members from tp[0][0][0] to tp[9][9][9]

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                for (int k = 0; k < 10; k++) {
                        tp[i][j][k].a = ++val;
                }
        }
}
  • Step 7 : Print values of structure members from tp[0][0][0] to tp[0][9][9]

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                for (int k = 0; k < 10; k++) {
                        printf("tp[%d][%d][%d].a = %d\n", i, j, k, tp[i][j][k].a);
                }
        }
}
  • Step 6.1 : Free array of structures pointed by all single pointers

for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
                free(tp[i][j]);
        }
}
  • Step 6.2 : Free array of single pointers

for (int i = 0; i < 10; i++) {
        free(tp[i]);
}
  • Step 6.3 : Free array of double pointers

free(tp);
  • See the full program below

 1#include <stdio.h>
 2#include <string.h>
 3
 4// Step 1 : Include stdlib.h
 5#include <stdlib.h>
 6
 7struct ABC {
 8        int a;
 9        int b;
10        int c;
11};
12
13int get_memory(struct ABC ****fp)
14{
15        // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9]
16        *fp = malloc(10 * sizeof(struct ABC **));
17
18        // Step 3.2 : Create array of single pointers for every double pointer
19        for (int i = 0; i < 10; i++) {
20                (*fp)[i] = malloc(10 * sizeof(struct ABC *));
21        }
22
23        // Step 3.3 : Create array of structures for every single pointer from (*fp)[0][9] ... (*fp)[9][9]
24        for (int i = 0; i < 10; i++) {
25                for (int j = 0; j < 10; j++) {
26                        (*fp)[i][j] = malloc(10 * sizeof(struct ABC));
27                }
28        }
29
30        return 0;
31}
32
33int main(void)
34{
35        // Step 2 : Define Triple Pointer
36        struct ABC ***tp;
37        int val = 0;
38
39        // Step 3 : Pass Triple pointer as reference
40        get_memory(&tp);
41
42        // Step 6 : Assing values to structure members from tp[0][0][0] to tp[9][9][9]
43        for (int i = 0; i < 10; i++) {
44                for (int j = 0; j < 10; j++) {
45                        for (int k = 0; k < 10; k++) {
46                                tp[i][j][k].a = ++val;
47                        }
48                }
49        }
50
51        // Step 7 : Print values of structure members from tp[0][0][0] to tp[0][9][9]
52        for (int i = 0; i < 10; i++) {
53                for (int j = 0; j < 10; j++) {
54                        for (int k = 0; k < 10; k++) {
55                                printf("tp[%d][%d][%d].a = %d\n", i, j, k, tp[i][j][k].a);
56                        }
57                }
58        }
59
60        // Step 6.1 : Free array of structures pointed by all single pointers
61        for (int i = 0; i < 10; i++) {
62                for (int j = 0; j < 10; j++) {
63                        free(tp[i][j]);
64                }
65        }
66
67        // Step 6.2 : Free array of single pointers
68        for (int i = 0; i < 10; i++) {
69                free(tp[i]);
70        }
71
72        // Step 6.3 : Free array of double pointers
73        free(tp);
74
75        return 0;
76}