Malloc struct Triple pointer ============================= * In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to allocate memory using malloc ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to allocate memory using calloc ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to allocate memory using realloc ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Create a 1,1,1 array ` * :ref:`Create a 1,1,10 array ` * :ref:`Create a 1,10,10 array ` * :ref:`Create a 10,10,10 array ` * :ref:`Pass Triple pointer by reference and allocate memory inside function ` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * 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 .. tab-set:: .. tab-item:: Homogeneous Arrays : Where Every Row has equal number of blocks * Example is arr[2][3][4] * Now let us take a look at real time examples .. _struct_tp_1x1x1_array: .. tab-set:: .. tab-item:: Create a 1,1,1 array * Step 1 : Include stdlib.h .. code-block:: c #include * Step 2 : Define Triple Pointer .. code-block:: c 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 .. code-block:: c ptr = malloc(sizeof(struct ABC **)); * Step 3.2 : ``*ptr`` is a double pointer and points to array of single pointers .. code-block:: c *ptr = malloc(sizeof(struct ABC *)); * Step 3.3 : ``**ptr`` is a single pointer and points to array of structures .. code-block:: c **ptr = malloc(sizeof(struct ABC)); * Step 4 : Assign a value to structure .. code-block:: c (***ptr).a = 65; OR .. code-block:: c (**ptr)[0].a = 65; OR .. code-block:: c (*ptr)[0][0].a = 65; OR .. code-block:: c ptr[0][0][0].a = 65; OR .. code-block:: c ptr[0][0]->a = 65; OR .. code-block:: c (*ptr[0])->a = 65; * Step 5 : Print the value of structure .. code-block:: c printf("(***ptr).a = %d\n", (***ptr).a); OR .. code-block:: c printf("(**ptr)[0].a = %d\n", (**ptr)[0].a); OR .. code-block:: c printf("(*ptr)[0][0].a = %d\n", (*ptr)[0][0].a); OR .. code-block:: c printf("ptr[0][0][0].a = %d\n", ptr[0][0][0].a); OR .. code-block:: c printf("ptr[0][0]->a = %d\n", ptr[0][0]->a); OR .. code-block:: c printf("(*ptr[0])->a = %d\n", (*ptr[0])->a); * Step 6.1 : Free array of structures .. code-block:: c free(**ptr); * Step 6.2 : Free array of single pointers .. code-block:: c free(*ptr); * Step 6.3 : Free array of double pointers .. code-block:: c free(ptr); * See the full program below .. code-block:: c :linenos: :emphasize-lines: 15, 18, 21, 24, 27, 28, 29, 30, 31, 32, 43, 46, 49 #include // Step 1 : Include stdlib.h #include struct ABC { int a; int b; int c; }; int main(void) { // Step 2 : Define Triple Pointer 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; (**ptr)[0].a = 65; (*ptr)[0][0].a = 65; ptr[0][0][0].a = 65; ptr[0][0]->a = 65; (*ptr[0])->a = 65; // Step 5 : Print the value of structure printf("(***ptr).a = %d\n", (***ptr).a); printf("(**ptr)[0].a = %d\n", (**ptr)[0].a); printf("(*ptr)[0][0].a = %d\n", (*ptr)[0][0].a); printf("ptr[0][0][0].a = %d\n", ptr[0][0][0].a); printf("ptr[0][0]->a = %d\n", ptr[0][0]->a); 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); return 0; } .. tab-set:: .. tab-item:: Summary of 1,1,1 array =========================================== ============================================================= 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 =========================================== ============================================================= .. _struct_tp_1x1x10_array: .. tab-set:: .. tab-item:: Create a 1,1,10 array * Step 1 : Include stdlib.h .. code-block:: c #include * Step 2 : Define Triple Pointer .. code-block:: c 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 .. code-block:: c ptr = malloc(sizeof(struct ABC **)); * Step 3.2 : \*ptr is a double pointer and points to array of single pointers .. code-block:: c *ptr = malloc(sizeof(struct ABC *)); * Step 3.3 : \*\*ptr is a single pointer and points to array of 10 structure objects .. code-block:: c **ptr = malloc(10 * sizeof(struct ABC)); * Step 4 : Assign a value to structure .. code-block:: c for (int i = 0; i < 10; i++) { ptr[0][0][i].a = ++val; } OR .. code-block:: c for (int i = 0; i < 10; i++) { (**ptr)[i].a = ++val; } OR .. code-block:: c for (int i = 0; i < 10; i++) { (*ptr)[0][i].a = ++val; } OR .. code-block:: c for (int i = 0; i < 10; i++) { ((**ptr) + i)->a = ++val; } OR .. code-block:: c for (int i = 0; i < 10; i++) { (ptr[0][0] + i)->a = ++val; } OR .. code-block:: c for (int i = 0; i < 10; i++) { ((*ptr)[0] + i)->a = ++val; } * Step 5 : Print the value of structure .. code-block:: c for (int i = 0; i < 10; i++) { printf("ptr[0][0][%d].a = %d\n", i, ptr[0][0][i].a); } OR .. code-block:: c for (int i = 0; i < 10; i++) { printf("(**ptr)[%d].a = %d\n", i, (**ptr)[i].a); } OR .. code-block:: c for (int i = 0; i < 10; i++) { printf("(*ptr)[0][%d].a = %d\n", i, (*ptr)[0][i].a); } OR .. code-block:: c for (int i = 0; i < 10; i++) { printf("((**ptr) + %d)->a = %d\n", i, ((**ptr) + i)->a); } OR .. code-block:: c for (int i = 0; i < 10; i++) { printf("(ptr[0][0] + %d)->a = %d\n", i, (ptr[0][0] + i)->a); } OR .. code-block:: c 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 .. code-block:: c free(**ptr); * Step 6.2 : Free array of single pointers .. code-block:: c free(*ptr); * Step 6.3 : Free array of double pointers .. code-block:: c free(ptr); * See the full program below .. code-block:: c :linenos: :emphasize-lines: 15, 19, 22, 25, 30, 35, 40, 45, 50, 55, 61, 66, 71, 76, 81, 86, 90, 93, 96 #include // Step 1 : Include stdlib.h #include struct ABC { int a; int b; int c; }; int main(void) { // Step 2 : Define Triple Pointer struct ABC ***ptr; int val = 0; // 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; } for (int i = 0; i < 10; i++) { (**ptr)[i].a = ++val; } for (int i = 0; i < 10; i++) { (*ptr)[0][i].a = ++val; } for (int i = 0; i < 10; i++) { ((**ptr) + i)->a = ++val; } for (int i = 0; i < 10; i++) { (ptr[0][0] + i)->a = ++val; } 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); } for (int i = 0; i < 10; i++) { printf("(**ptr)[%d].a = %d\n", i, (**ptr)[i].a); } for (int i = 0; i < 10; i++) { printf("(*ptr)[0][%d].a = %d\n", i, (*ptr)[0][i].a); } for (int i = 0; i < 10; i++) { printf("((**ptr) + %d)->a = %d\n", i, ((**ptr) + i)->a); } for (int i = 0; i < 10; i++) { printf("(ptr[0][0] + %d)->a = %d\n", i, (ptr[0][0] + i)->a); } 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); return 0; } .. tab-set:: .. tab-item:: Summary of 1,1,10 array =========================================== ============================================================= 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 =========================================== ============================================================= .. _struct_tp_1x10x10_array: .. tab-set:: .. tab-item:: Create a 1,10,10 array * Step 1 : Include stdlib.h .. code-block:: c #include * Step 2 : Define Triple Pointer .. code-block:: c 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]`` .. code-block:: c ptr = malloc(sizeof(struct ABC **)); * Step 3.2 : Create array of single pointers from ``ptr[0][0]`` ... ``ptr[0][9]`` .. code-block:: c *ptr = malloc(10 * sizeof(struct ABC *)); OR .. code-block:: c ptr[0] = malloc(10 * sizeof(struct ABC *)); * Step 3.3 : Create array of structures from ``ptr[0][0][0]`` to ``ptr[0][9][9]`` .. code-block:: c for (int i = 0; i < 10; i++) { (*ptr)[i] = malloc(10 * sizeof(struct ABC)); } OR .. code-block:: c 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]`` .. code-block:: c 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]`` .. code-block:: c 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 .. code-block:: c for (int i = 0; i < 10; i++) { free((*ptr)[i]); } * Step 6.2 : Free array of single pointers .. code-block:: c free(*ptr); * Step 6.3 : Free array of double pointers .. code-block:: c free(ptr); * See the full program below .. code-block:: c :linenos: :emphasize-lines: 16, 20, 23, 27, 34, 42, 48, 52, 55 #include #include // Step 1 : Include stdlib.h #include struct ABC { int a; int b; int c; }; int main(void) { // Step 2 : Define Triple Pointer struct ABC ***ptr; int val = 0; // 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 *)); // 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)); } // 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][%d][%d].a = %d\n", i, j, 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); return 0; } .. _struct_tp_10x10x10_array: .. tab-set:: .. tab-item:: Create a 10,10,10 array * Step 1 : Include stdlib.h .. code-block:: c #include * Step 2 : Define Triple Pointer .. code-block:: c 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]`` .. code-block:: c ptr = malloc(10 * sizeof(struct ABC **)); * Step 3.2 : Create array of single pointers from ``ptr[0][0]`` to ``ptr[9][9]`` .. code-block:: c 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]`` .. code-block:: c 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]`` .. code-block:: c 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]`` .. code-block:: c 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]`` .. code-block:: c 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]`` .. code-block:: c for (int i = 0; i < 10; i++) { free(ptr[i]); } * Step 6.3 : Free array of double pointers .. code-block:: c free(ptr); * See the full program below .. code-block:: c :linenos: :emphasize-lines: 16, 20, 25, 31, 39, 48, 56, 63, 67 #include #include // Step 1 : Include stdlib.h #include struct ABC { int a; int b; int c; }; int main(void) { // Step 2 : Define Triple Pointer struct ABC ***ptr; int val = 0; // 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]`` ... ``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]`` ... ``ptr[9][9]`` for (int i = 0; i < 10; i++) { free(ptr[i]); } // Step 6.3 : Free array of double pointers. In this case, free ten double pointers from ``ptr[0]`` to ``ptr[9]`` free(ptr); return 0; } .. _struct_tp_tp_passed_by_reference: .. tab-set:: .. tab-item:: Pass Triple pointer by reference and allocate memory inside function * Step 1 : Include stdlib.h .. code-block:: c #include * Step 2 : Define an allocation function .. code-block:: c :emphasize-lines: 1 int get_memory(struct ABC ****fp) { return 0; } * Step 3.1 : Create array of double pointers from ``(*fp)[0]`` ... ``(*fp)[9]`` .. code-block:: c :emphasize-lines: 4 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 .. code-block:: c :emphasize-lines: 7,8,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 *)); } return 0; } * Step 3.3 : Create array of structures for every single pointer from ``(*fp)[0][9]`` ... ``(*fp)[9][9]`` .. code-block:: c :emphasize-lines: 12,13,14,15,16 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 .. code-block:: c struct ABC { int a; int b; int c; }; struct ABC ***tp; * Step 5 : Pass Triple pointer as reference .. code-block:: c get_memory(&tp); * Step 6 : Assing values to structure members from tp[0][0][0] to tp[9][9][9] .. code-block:: c 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] .. code-block:: c 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 .. code-block:: c 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 .. code-block:: c for (int i = 0; i < 10; i++) { free(tp[i]); } * Step 6.3 : Free array of double pointers .. code-block:: c free(tp); * See the full program below .. code-block:: c :linenos: :emphasize-lines: 16, 20, 26, 40, 46, 55, 63, 69, 73 #include #include // Step 1 : Include stdlib.h #include struct ABC { int a; int b; int c; }; 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; } int main(void) { // Step 2 : Define Triple Pointer struct ABC ***tp; int val = 0; // Step 3 : 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); return 0; } .. card:: See Also * Current Module * :doc:`../malloc_ptr` * Previous Module * :doc:`../../array_n_ptrs/array_n_ptrs` * Next Module * :doc:`../../typecasting_n_ptr/typecasting_n_ptr` * Other Modules * :doc:`../../variable_and_ptr/variable_and_ptr` * :doc:`../../funcs_n_ptrs/funcs_n_ptrs` * :doc:`../../memcpy_ptr/memcpy_ptr` * :doc:`../../const_ptr/const_ptr` * :doc:`../../void_ptr/void_ptr` * :doc:`../../array_of_ptr/array_of_ptr` * :doc:`../../ptr_to_array/ptr_to_array` * :doc:`../../function_ptr/function_ptr` * :doc:`../../pre_incr_ptr/pre_incr_ptr` * :doc:`../../post_incr_ptr/post_incr_ptr` * :doc:`../../pre_decr_ptr/pre_decr_ptr` * :doc:`../../post_decr_ptr/post_decr_ptr`