Array of char Single Pointers ============================== In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to use single pointer with a variable ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to use single pointer with an array of characters ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to use single pointer with heap ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is the syntax for Array of Single Pointers ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Basics of Array of Single Pointers * :ref:`Array of character pointers ` * :ref:`Example 1 : Array of character pointers : Pointers pointing to variables ` * :ref:`Example 2 : Array of character pointers : Pointers pointing to single dimension arrays ` * :ref:`Example 3 : Array of character pointers : Pointers pointing to Heap ` * :ref:`Example 4 : Array of character pointers : Pointers pointing to Read only Strings ` * :ref:`Example 5 : Array of character pointers : Pointers pointing to Single dimension rows inside Double dimension arrays ` * :ref:`Example 6: Array of character pointers : Combine all the above ` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Functions and Array of Single Pointers * :ref:`Example 7 : Array of character pointers : Pass by Value : Without typedef ` * :ref:`Example 8 : Array of character pointers : Pass by Value : With typedef ` * :ref:`Example 9 : Array of character pointers : Pass by Reference : Without typedef ` * :ref:`Example 10 : Array of character pointers : Pass by Reference : With typedef ` .. _array_of_ptr_char_sp_ex7: .. tab-set:: .. tab-item:: Array of character pointers .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Syntax : char * ptr [ ] = { p1, p2, p3 , etc., }; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * ``ptr`` is array of character pointers .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * ptr[0], ptr[1], ptr[2] are single pointers ======================================================== =============== What can we do with Single pointer ? ======================================================== =============== * can hold address of a simple character variable * can point to start of single dimension array * can point to memory in heap * can point to read only string * can point to a row in a double dimension array ======================================================== =============== ========================================================================== =============== What can we do with ptr[0], ptr[1] ? ========================================================================== =============== ptr[0], ptr[1], ptr[2] ... are single pointers ptr[0], ptr[1], ptr[2] can hold address of a simple character variable ptr[0], ptr[1], ptr[2] can point to start of single dimension array ptr[0], ptr[1], ptr[2] can point to memory in heap ptr[0], ptr[1], ptr[2] can point to read only string ptr[0], ptr[1], ptr[2] can point to a row in a double dimension array ========================================================================== =============== .. _array_of_ptr_char_sp_ex8: .. tab-set:: .. tab-item:: Example 1 : Array of character pointers : Pointers pointing to variables * Step 1 : Define an array of pointers .. code-block:: c char x = 65; char y = 66; char z = 67; char *ptr[] = { &x, &y, &z }; * Step 2 : Read variables using pointers .. code-block:: c printf("x = %d\n", *ptr[0]); printf("y = %d\n", *ptr[1]); printf("z = %d\n", *ptr[2]); * Step 3 : Write variables using pointers .. code-block:: c *ptr[0] = 100; // changes x *ptr[1] = 101; // changes y *ptr[2] = 102; // changes z * See full program below .. code-block:: c #include int main(void) { char x = 65; char y = 66; char z = 67; char *ptr[] = { &x, &y, &z }; printf("x = %d\n", *ptr[0]); printf("y = %d\n", *ptr[1]); printf("z = %d\n", *ptr[2]); *ptr[0] = 100; *ptr[1] = 101; *ptr[2] = 102; printf("x = %d\n", *ptr[0]); printf("y = %d\n", *ptr[1]); printf("z = %d\n", *ptr[2]); return 0; } .. _array_of_ptr_char_sp_ex9: .. tab-set:: .. tab-item:: Example 2 : Array of character pointers : Pointers pointing to single dimension arrays * Step 1 : Define an array of pointers .. code-block:: c char arr0[] = "Laptop"; char arr1[] = "Mouse"; char arr2[] = "SCSI"; char *ptr[] = { arr0, arr1, arr2 }; * Step 2 : Read variables using pointers .. code-block:: c printf("arr0 = %s\n", ptr[0]); printf("arr1 = %s\n", ptr[1]); printf("arr2 = %s\n", ptr[2]); OR .. code-block:: c //Iterate array of pointers using pointer for (int i = 0; i < 3; i++) { printf("String %d : %s\n", i, ptr[i]); } * Step 3 : Write variables using pointers .. code-block:: c strcpy(ptr[1], "Keyboard"); * See full program below .. code-block:: c #include #include #include int main(void) { char arr0[10] = "Laptop"; char arr1[10] = "Mouse"; char arr2[10] = "SCSI"; //ptr[0] is pointing to arr0 //ptr[1] is pointing to arr1 //ptr[2] is pointing to arr2 char *ptr[] = { arr0, arr1, arr2 }; //Read using pointers printf("arr0 = %s\n", ptr[0]); printf("arr1 = %s\n", ptr[1]); printf("arr2 = %s\n", ptr[2]); //Write using pointers strcpy(ptr[1], "Keyboard"); //Iterate array of pointers using pointer for (int i = 0; i < 3; i++) { printf("String %d : %s\n", i, ptr[i]); } return 0; } .. _array_of_ptr_char_sp_ex10: .. tab-set:: .. tab-item:: Example 3 : Array of character pointers : Pointers pointing to Heap * Step 1 : Define an array of pointers .. code-block:: c char *p1 = malloc(10); char *p2 = malloc(10); char *p3 = malloc(10); char *ptr[] = { p1, p2, p3 }; * Step 2 : Write variables using array of pointers .. code-block:: c strcpy(ptr[0], "Laptop"); strcpy(ptr[1], "Keyboard"); strcpy(ptr[2], "Mouse"); * Step 3 : Read variables using array of pointers .. code-block:: c for (int i = 0;i < 3; i++) { printf("String : %d : %s\n", i, ptr[i]); } * Step 4 : Free Heap using using array of pointers .. code-block:: c for (int i = 0;i < 3; i++) { free(ptr[i]); } * See full program below .. code-block:: c #include #include #include int main(void) { char *p1 = malloc(10); char *p2 = malloc(10); char *p3 = malloc(10); char *ptr[] = { p1, p2, p3 }; //Write heap using array of pointers strcpy(ptr[0], "Laptop"); strcpy(ptr[1], "Keyboard"); strcpy(ptr[2], "Mouse"); //Read heap using array of pointers for (int i = 0;i < 3; i++) { printf("String : %d : %s\n", i, ptr[i]); } //Free heap using array of pointers for (int i = 0;i < 3; i++) { free(ptr[i]); } return 0; } .. _array_of_ptr_char_sp_ex11: .. tab-set:: .. tab-item:: Example 4 : Array of character pointers : Pointers pointing to Read only Strings * Step 1 : Define an array of pointers .. code-block:: c char *ptr[] = { "Laptop", "Mouse", "SCSI" }; * Step 2 : Read variables using pointers .. code-block:: c for (int i = 0; i < 3 ; i++) { printf("String %d : %s\n", i, ptr[i]); } * Step 3 : Write variables using pointers .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Not possible ! Since pointers are pointing to read only strings ! * See full program below .. code-block:: c #include #include int main(void) { char *ptr[] = { "Laptop", "Mouse", "SCSI" }; for (int i = 0; i < 3 ; i++) { printf("String %d : %s\n", i, ptr[i]); } return 0; } .. _array_of_ptr_char_sp_ex12: .. tab-set:: .. tab-item:: Example 5 : Array of character pointers : Pointers pointing to Single dimension rows inside Double dimension arrays * Step 1 : Define an array of pointers .. code-block:: c char buffers[3][10] = { "Laptop", "Mouse", "SCSI" }; char *ptr[] = { buffers[0], buffers[1], buffers[2] }; * Step 2 : Read variables using array of pointers .. code-block:: c for (int i = 0; i < 3 ; i++) { printf("String %d : %s\n", i, ptr[i]); } * Step 3 : Write variables using array of pointers .. code-block:: c strcpy(ptr[2], "Keyboard"); * See full program below .. code-block:: c #include #include int main(void) { char buffers[3][10] = { "Laptop", "Mouse", "SCSI" }; char *ptr[] = { buffers[0], buffers[1], buffers[2] }; strcpy(ptr[2], "Keyboard"); for (int i = 0; i < 3 ; i++) { printf("String %d : %s\n", i, ptr[i]); } return 0; } .. _array_of_ptr_char_sp_ex13: .. tab-set:: .. tab-item:: Example 6 : Array of character pointers : Combine all the above * Step 1 : Define an array of pointers .. code-block:: c char *ptr[] = {&x, p, arr, str, hstr, buffers[1] }; Here there are 6 pointers. See below ================== =============================================================== Pointer ================== =============================================================== ``ptr[0]`` Holds address of variable ``x`` ``ptr[1]`` Holds address of variable ``y`` ``ptr[2]`` Holds startiing address of array ``arr`` ``ptr[3]`` Holds starting address of readonly string ``str`` ``ptr[4]`` Holds starting address of memory in heap ``hstr`` ``ptr[5]`` Holds starting address of single dimension array ``buffers[1]`` ================== =============================================================== * Step 2 : Use ``ptr[0]`` to access contents of ``x`` .. code-block:: c // Access variable "x" using ptr[0] printf("First character in ptr[0] = %c\n", *ptr[0]); printf("First character in ptr[0] = %c\n", ptr[0][0]); * Step 3 : Use ``ptr[1]`` to access contents of ``y`` .. code-block:: c // Access variable "y" using ptr[1] printf("First character in ptr[1] = %c\n", *ptr[1]); printf("First character in ptr[1] = %c\n", ptr[1][0]); * Step 4 : Use ``ptr[2]`` to access contents of ``arr`` .. code-block:: c // Access full string present in "arr" using ptr[2] printf("Printing arr using ptr[2] : %s\n", ptr[2]); //Access individual characters in "arr" using ptr[2] for (int i = 0; i < strlen(ptr[2]); i++) { printf("ptr[2][%d] = %c\n", i, ptr[2][i]); } * Step 5 : Use ``ptr[3]`` to access contents of ``str`` .. code-block:: c // Access full string present in "arr" using ptr[3] printf("Printing str using ptr[3] : %s\n", ptr[3]); //Access individual characters in "arr" using ptr[3] for (int i = 0; i < strlen(ptr[3]); i++) { printf("ptr[3][%d] = %c\n", i, ptr[3][i]); } * Step 6 : Use ``ptr[4]`` to access contents of ``hstr`` .. code-block:: c // Access full string present in "arr" using ptr[4] printf("Printing hstr using ptr[4] : %s\n", ptr[4]); //Access individual characters in "arr" using ptr[4] for (int i = 0; i < strlen(ptr[4]); i++) { printf("ptr[4][%d] = %c\n", i, ptr[4][i]); } * Step 7 : Use ``ptr[5]`` to access contents of ``buffers[1]`` .. code-block:: c // Access full string present in "arr" using ptr[5] printf("Printing buffers[1] using ptr[5] : %s\n", ptr[5]); //Access individual characters in "arr" using ptr[5] for (int i = 0; i < strlen(ptr[5]); i++) { printf("ptr[5][%d] = %c\n", i, ptr[5][i]); } * See full program below .. code-block:: c #include #include #include int main(void) { char x = 65; char y = 66; char *p = &y; char arr[] = "Laptop"; char *str = "Mouse"; char *hstr = malloc(10 * sizeof(char)); strcpy(hstr, "scsi"); char buffers[][10] = {"Home", "House", "Palace" }; char *ptr[] = {&x, p, arr, str, hstr, buffers[1] }; // Access variable "x" using ptr[0] printf("First character in ptr[0] = %c\n", *ptr[0]); printf("First character in ptr[0] = %c\n", ptr[0][0]); // Access variable "y" using ptr[1] printf("First character in ptr[1] = %c\n", *ptr[1]); printf("First character in ptr[1] = %c\n", ptr[1][0]); // Access full string present in "arr" using ptr[2] printf("Printing arr using ptr[2] : %s\n", ptr[2]); //Access individual characters in "arr" using ptr[2] for (int i = 0; i < strlen(ptr[2]); i++) { printf("ptr[2][%d] = %c\n", i, ptr[2][i]); } // Access full string present in "arr" using ptr[3] printf("Printing str using ptr[3] : %s\n", ptr[3]); //Access individual characters in "arr" using ptr[3] for (int i = 0; i < strlen(ptr[3]); i++) { printf("ptr[3][%d] = %c\n", i, ptr[3][i]); } // Access full string present in "arr" using ptr[4] printf("Printing hstr using ptr[4] : %s\n", ptr[4]); //Access individual characters in "arr" using ptr[4] for (int i = 0; i < strlen(ptr[4]); i++) { printf("ptr[4][%d] = %c\n", i, ptr[4][i]); } // Access full string present in "arr" using ptr[5] printf("Printing buffers[1] using ptr[5] : %s\n", ptr[5]); //Access individual characters in "arr" using ptr[5] for (int i = 0; i < strlen(ptr[5]); i++) { printf("ptr[5][%d] = %c\n", i, ptr[5][i]); } free(hstr); return 0; } .. _array_of_ptr_char_sp_ex14: .. tab-set:: .. tab-item:: Example 7 : Array of character pointers : Pass by Value : Without typedef * Step 1 : Define an array of pointers .. code-block:: c char arr0[10] = "Laptop"; char arr1[10] = "Mouse"; char arr2[10] = "SCSI"; char *ptr[] = { arr0, arr1, arr2 }; * Step 2 : Pass array of pointers by value .. code-block:: c fun(ptr); * Step 3 : Define a function which takes array of pointers as parameter .. code-block:: c void fun(char *ptr_v[]) { } * Step 4 : Read variables using array of pointers inside function .. code-block:: c for (int i = 0; i < 3; i++) { printf("String %d : %s\n", i, ptr_v[i]); } * Step 5 : Write variables using array of pointers inside function .. code-block:: c strcpy(ptr_v[1], "Keyboard"); * See full program below .. code-block:: c #include #include #include void fun(char *ptr_v[]) { //Read using pointers printf("arr0 = %s\n", ptr_v[0]); printf("arr1 = %s\n", ptr_v[1]); printf("arr2 = %s\n", ptr_v[2]); //Write using pointers strcpy(ptr_v[1], "Keyboard"); //Iterate array of pointers using pointer for (int i = 0; i < 3; i++) { printf("String %d : %s\n", i, ptr_v[i]); } } int main(void) { char arr0[10] = "Laptop"; char arr1[10] = "Mouse"; char arr2[10] = "SCSI"; //ptr[0] is pointing to arr0 //ptr[1] is pointing to arr1 //ptr[2] is pointing to arr2 char *ptr[] = { arr0, arr1, arr2 }; fun(ptr); return 0; } .. _array_of_ptr_char_sp_ex15: .. tab-set:: .. tab-item:: Example 8 : Array of character pointers : Pass by Value : With typedef * Step 1 : Define a type for array of pointers .. code-block:: c typedef char *tptr[]; * Step 1 : Define an array of pointers using new type ``tptr`` .. code-block:: c tptr ptr = { arr0, arr1, arr2 }; * Step 2 : Pass array of pointers by Value .. code-block:: c fun(ptr); * Step 3 : Define a function which takes array of pointers as parameter .. code-block:: c void fun(tptr ptr_v) { } * Step 4 : Read variables using array of pointers inside function .. code-block:: c printf("arr0 = %s\n", ptr_v[0]); printf("arr1 = %s\n", ptr_v[1]); printf("arr2 = %s\n", ptr_v[2]); * Step 5 : Write variables using array of pointers inside function .. code-block:: c strcpy(ptr_v[1], "Keyboard"); * See full program below .. code-block:: c #include #include #include typedef char *tptr[]; void fun(tptr ptr_v) { //Read using pointers printf("arr0 = %s\n", ptr_v[0]); printf("arr1 = %s\n", ptr_v[1]); printf("arr2 = %s\n", ptr_v[2]); //Write using pointers strcpy(ptr_v[1], "Keyboard"); //Iterate array of pointers using pointer for (int i = 0; i < 3; i++) { printf("String %d : %s\n", i, ptr_v[i]); } } int main(void) { char arr0[10] = "Laptop"; char arr1[10] = "Mouse"; char arr2[10] = "SCSI"; tptr ptr = { arr0, arr1, arr2 }; fun(ptr); return 0; } .. _array_of_ptr_char_sp_ex16: .. tab-set:: .. tab-item:: Example 9 : Array of character pointers : Pass by Reference : Without typedef * Step 1 : Define array of pointers .. code-block:: c char arr0[10] = "Laptop"; char arr1[10] = "Mouse"; char arr2[10] = "SCSI"; char *ptr[] = { arr0, arr1, arr2 }; * Step 2 : Pass array of pointers by reference to a function .. code-block:: c fun(&ptr); * Step 3 : Define the function ``fun`` and perform read, write .. code-block:: c void fun(char * (*ptr_r)[]) { //Read using pointers printf("arr0 = %s\n", (*ptr_r)[0]); printf("arr1 = %s\n", (*ptr_r)[1]); printf("arr2 = %s\n", (*ptr_r)[2]); //Write using pointers strcpy((*ptr_r)[1], "Keyboard"); //Iterate array of pointers using pointer for (int i = 0; i < 3; i++) { printf("String %d : %s\n", i, (*ptr_r)[i]); } } * See full program below .. code-block:: c #include #include #include void fun(char * (*ptr_r)[]) { //Read using pointers printf("arr0 = %s\n", (*ptr_r)[0]); printf("arr1 = %s\n", (*ptr_r)[1]); printf("arr2 = %s\n", (*ptr_r)[2]); //Write using pointers strcpy((*ptr_r)[1], "Keyboard"); //Iterate array of pointers using pointer for (int i = 0; i < 3; i++) { printf("String %d : %s\n", i, (*ptr_r)[i]); } } int main(void) { char arr0[10] = "Laptop"; char arr1[10] = "Mouse"; char arr2[10] = "SCSI"; //ptr[0] is pointing to arr0 //ptr[1] is pointing to arr1 //ptr[2] is pointing to arr2 char *ptr[] = { arr0, arr1, arr2 }; fun(&ptr); return 0; } .. _array_of_ptr_char_sp_ex17: .. tab-set:: .. tab-item:: Example 10 : Array of character pointers : Pass by Reference : With typedef * Step 1 : Define the type for array of pointers .. code-block:: c typedef char *tptr[]; * Step 2 : Define array of pointers .. code-block:: c char arr0[10] = "Laptop"; char arr1[10] = "Mouse"; char arr2[10] = "SCSI"; tptr ptr = { arr0, arr1, arr2 }; * Step 3 : Pass array of pointers by reference to a function .. code-block:: c fun(&ptr); * Step 4 : Define the function ``fun1`` and perform read, write .. code-block:: c void fun(tptr *ptr_r) { //Read using pointers printf("arr0 = %s\n", (*ptr_r)[0]); printf("arr1 = %s\n", (*ptr_r)[1]); printf("arr2 = %s\n", (*ptr_r)[2]); //Write using pointers strcpy((*ptr_r)[1], "Keyboard"); //Iterate array of pointers using pointer for (int i = 0; i < 3; i++) { printf("String %d : %s\n", i, (*ptr_r)[i]); } } * See full program below .. code-block:: c #include #include #include typedef char *tptr[]; void fun(tptr *ptr_r) { //Read using pointers printf("arr0 = %s\n", (*ptr_r)[0]); printf("arr1 = %s\n", (*ptr_r)[1]); printf("arr2 = %s\n", (*ptr_r)[2]); //Write using pointers strcpy((*ptr_r)[1], "Keyboard"); //Iterate array of pointers using pointer for (int i = 0; i < 3; i++) { printf("String %d : %s\n", i, (*ptr_r)[i]); } } int main(void) { char arr0[10] = "Laptop"; char arr1[10] = "Mouse"; char arr2[10] = "SCSI"; tptr ptr = { arr0, arr1, arr2 }; fun(&ptr); return 0; } .. card:: See Also * Current Module * :doc:`../array_of_ptr` * Previous Module * :doc:`../../void_ptr/void_ptr` * Next Module * :doc:`../../ptr_to_array/ptr_to_array` * Other Modules * :doc:`../../variable_and_ptr/variable_and_ptr` * :doc:`../../array_n_ptrs/array_n_ptrs` * :doc:`../../malloc_ptr/malloc_ptr` * :doc:`../../typecasting_n_ptr/typecasting_n_ptr` * :doc:`../../funcs_n_ptrs/funcs_n_ptrs` * :doc:`../../memcpy_ptr/memcpy_ptr` * :doc:`../../const_ptr/const_ptr` * :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`