Malloc char 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 characters .. 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 .. _char_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 char ***ptr; * Step 3.1 : ``ptr`` is a triple pointer and points to array of double pointers .. code-block:: c ptr = malloc(sizeof(char **)); * Step 3.2 : ``*ptr`` is a double pointer and points to array of single pointers .. code-block:: c *ptr = malloc(sizeof(char *)); * Step 3.3 : ``**ptr`` is a single pointer and points to array of characters .. code-block:: c **ptr = malloc(sizeof(char)); * Step 4 : Assign a value to character .. code-block:: c ***ptr = 65; * Step 5 : Print the value of character .. code-block:: c printf("***ptr = %d\n", ***ptr); * Step 6.1 : Free array of characters .. 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: 9, 12, 15, 18, 21, 28, 31, 34 #include // Step 1 : Include stdlib.h #include int main(void) { // Step 2 : Define Triple Pointer char ***ptr; // Step 3.1 : ptr is a triple pointer and points to array of double pointers ptr = malloc(sizeof(char **)); // Step 3.2 : *ptr is a double pointer and points to array of single pointers *ptr = malloc(sizeof(char *)); // Step 3.3 : **ptr is a single pointer and points to array of characters **ptr = malloc(sizeof(char)); // Step 4 : Assign a value to character ***ptr = 65; // Step 5 : Print the value of character printf("***ptr = %d\n", ***ptr); // Step 6.1 : Free array of characters 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 character 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) * 1 Byte * Fully dereferenced * Hence ``***ptr`` is a character * sizeof(character) is 1 Byte always ptr = malloc( sizeof(char \*\*) ) * ``ptr`` points to array of double pointers \*ptr = malloc( sizeof(char \*) ) * ``*ptr`` points to array of single pointers \*\*ptr = malloc( sizeof(char ) ) * ``**ptr`` points to array of characters =========================================== ============================================================= .. _char_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 char ***ptr; * Step 3.1 : ``ptr`` is a triple pointer and points to array of double pointers .. code-block:: c ptr = malloc(sizeof(char **)); * Step 3.2 : ``*ptr`` is a double pointer and points to array of single pointers .. code-block:: c *ptr = malloc(sizeof(char *)); * Step 3.3 : ``**ptr`` is a single pointer and points to array of characters .. code-block:: c **ptr = malloc(10 * sizeof(char)); * Step 4 : ``**ptr`` is now pointing to array of 10 characters. Hence can use it for string operations .. code-block:: c strcpy(**ptr, "Laptop"); * Step 5 : Print the string .. code-block:: c printf("**ptr = %s\n", **ptr); * Step 6.1 : Free array of characters .. 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: 10, 13, 16, 19, 22, 29, 34, 38, 41, 44 #include #include // Step 1 : Include stdlib.h #include int main(void) { // Step 2 : Define Triple Pointer char ***ptr; // Step 3.1 : ptr is a triple pointer and points to array of double pointers ptr = malloc(sizeof(char **)); // Step 3.2 : *ptr is a double pointer and points to array of single pointers *ptr = malloc(sizeof(char *)); // Step 3.3 : **ptr is a single pointer and points to array of characters **ptr = malloc(10 * sizeof(char)); // Step 4 : **ptr is now pointing to array of 10 characters. Hence can use it for string operations strcpy(**ptr, "Laptop"); // Step 5 : Print the string printf("**ptr = %s\n", **ptr); for (int i = 0; i < strlen(**ptr); i++) { printf("(**ptr)[i] = %c\n", (**ptr)[i]); } for (int i = 0; i < strlen(**ptr); i++) { printf("ptr[0][0][i] = %c\n", ptr[0][0][i]); } // Step 6.1 : Free array of characters 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 character 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) * 1 Byte * Fully dereferenced * Hence ``***ptr`` is a character * sizeof(character) is 1 Byte always ptr = malloc( sizeof(char \*\*) ) * ``ptr`` points to array of double pointers \*ptr = malloc( sizeof(char \*) ) * ``*ptr`` points to array of single pointers \*\*ptr = malloc( 10 * sizeof(char ) ) * ``**ptr`` points to array of 10 characters strcpy(\*\*ptr, "Laptop"); * Copies string "Laptop" to \*ptr (\*\*ptr)[i] * Method 1 : To access individual characters ptr[0][0][i] * Method 2 : To access individual characters =========================================== ============================================================= .. _char_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 char ***ptr; * Step 3.1 : ptr is a triple pointer and points to array of double pointers .. code-block:: c ptr = malloc(sizeof(char **)); * Step 3.2 : Create array of single pointers from ``(*ptr)[0]`` ... ``(*ptr)[9]`` .. code-block:: c *ptr = malloc(10 * sizeof(char *)); * Step 3.3 : Now create array of characters for every single pointer from ``(*ptr)[0]`` ... ``(*ptr)[9]`` .. code-block:: c for (int i = 0; i < 10; i++) { (*ptr)[i] = malloc(10 * sizeof(char)); } * Step 4 : ``(*ptr)[0]`` .. ``(*ptr)[9]`` is now pointing to array of characters each .. code-block:: c for (int i = 0; i < 10; i++) { sprintf((*ptr)[i], "Laptop %d", i); } * Step 5 : Print the strings .. code-block:: c for (int i = 0; i < 10; i++) { printf("(*ptr)[i] = %s\n", (*ptr)[i]); } * Step 6.1 : Free array of characters 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: 10, 13, 18, 23, 28, 33, 38, 42, 45 #include #include // Step 1 : Include stdlib.h #include int main(void) { // Step 2 : Define Triple Pointer char ***ptr; // Step 3.1 : ptr is a triple pointer and points to array of double pointers ptr = malloc(sizeof(char **)); // Step 3.2 // *ptr is a double pointer and points to array of single pointers // Create array of single pointers from (*ptr)[0] ... (*ptr)[9] *ptr = malloc(10 * sizeof(char *)); // Step 3.3 : Now create array of characters for every single pointer from (*ptr)[0] ... (*ptr)[9] for (int i = 0; i < 10; i++) { (*ptr)[i] = malloc(10 * sizeof(char)); } // Step 4 : (*ptr)[0] .. (*ptr)[9] is now pointing to array of characters each for (int i = 0; i < 10; i++) { sprintf((*ptr)[i], "Laptop %d", i); } // Step 5 : Print the strings for (int i = 0; i < 10; i++) { printf("(*ptr)[i] = %s\n", (*ptr)[i]); } // Step 6.1 : Free array of characters 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; } .. _char_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 char ***ptr; * Step 3.1 : Create array of double pointers from ptr[0] ... ptr[9] .. code-block:: c ptr = malloc(10 * sizeof(char **)); * Step 3.2 : Create array of single pointers for every double pointer .. code-block:: c for (int i = 0; i < 10; i++) { ptr[i] = malloc(10 * sizeof(char *)); } * Step 3.3 : Create array of characters for every single pointer from ptr[0][9] ... ptr[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(char)); } } * Step 4 : Use ptr[0][0] ... ptr[9][9] as character array .. code-block:: c for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { sprintf(ptr[i][j], "Laptop%d%d", i,j); } } * Step 5 : Print the strings .. code-block:: c for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { printf("ptr[%d][%d] = %s\n", i, j, ptr[i][j]); } } * Step 6.1 : Free array of characters pointed by all single pointers .. 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 .. 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: 10, 13, 17, 23, 30, 37, 44, 50, 54 #include #include // Step 1 : Include stdlib.h #include int main(void) { // Step 2 : Define Triple Pointer char ***ptr; // Step 3.1 : Create array of double pointers from ptr[0] ... ptr[9] ptr = malloc(10 * sizeof(char **)); // Step 3.2 : Create array of single pointers for every double pointer for (int i = 0; i < 10; i++) { ptr[i] = malloc(10 * sizeof(char *)); } // Step 3.3 : Create array of characters for every single pointer from ptr[0][9] ... ptr[9][9] for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { ptr[i][j] = malloc(10 * sizeof(char)); } } // Step 4 : Use ptr[0][0] ... ptr[9][9] as character array for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { sprintf(ptr[i][j], "Laptop%d%d", i,j); } } // Step 5 : Print the strings for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { printf("ptr[%d][%d] = %s\n", i, j, ptr[i][j]); } } // Step 6.1 : Free array of characters pointed by all single pointers 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 for (int i = 0; i < 10; i++) { free(ptr[i]); } // Step 6.3 : Free array of double pointers free(ptr); return 0; } .. _char_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(char ****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(char ****fp) { // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9] *fp = malloc(10 * sizeof(char **)); 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(char ****fp) { // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9] *fp = malloc(10 * sizeof(char **)); // Step 3.2 : Create array of single pointers for every double pointer for (int i = 0; i < 10; i++) { (*fp)[i] = malloc(10 * sizeof(char *)); } return 0; } * Step 3.3 : Create array of characters for every single pointer from ``(*fp)[0][9]`` ... ``(*fp)[9][9]`` .. code-block:: c :emphasize-lines: 12,13,14,15,16 int get_memory(char ****fp) { // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9] *fp = malloc(10 * sizeof(char **)); // Step 3.2 : Create array of single pointers for every double pointer for (int i = 0; i < 10; i++) { (*fp)[i] = malloc(10 * sizeof(char *)); } // Step 3.3 : Create array of characters 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(char)); } } return 0; } * Step 4 : Define Triple Pointer in caller .. code-block:: c char ***tp; * Step 5 : Pass Triple pointer as reference .. code-block:: c get_memory(&tp); * Step 6 : Use tp[0][0] ... tp[9][9] as character array .. code-block:: c for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { sprintf(tp[i][j], "Laptop%d%d", i,j); } } * Step 7 : Print the strings .. code-block:: c for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { printf("tp[%d][%d] = %s\n", i, j, tp[i][j]); } } * Step 6.1 : Free array of characters 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: 10, 14, 20, 30, 33, 38, 45, 52, 58, 62 #include #include // Step 1 : Include stdlib.h #include int get_memory(char ****fp) { // Step 3.1 : Create array of double pointers from (*fp)[0] ... (*fp)[9] *fp = malloc(10 * sizeof(char **)); // Step 3.2 : Create array of single pointers for every double pointer for (int i = 0; i < 10; i++) { (*fp)[i] = malloc(10 * sizeof(char *)); } // Step 3.3 : Create array of characters 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(char)); } } return 0; } int main(void) { // Step 2 : Define Triple Pointer char ***tp; // Step 3 : Pass Triple pointer as reference get_memory(&tp); // Step 4 : Use tp[0][0] ... tp[9][9] as character array for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { sprintf(tp[i][j], "Laptop%d%d", i,j); } } // Step 5 : Print the strings for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { printf("tp[%d][%d] = %s\n", i, j, tp[i][j]); } } // Step 6.1 : Free array of characters 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`