Basics of Character 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 Basics of Single pointers * :ref:`Single pointer and a variable : *p notation ` * :ref:`Single pointer and a variable : p[0] notation ` * :ref:`Single pointer and an array ` * :ref:`Single pointer and Heap memory ` * :ref:`Single pointer and a string ` * :ref:`Single pointer and relative memory access ` * :ref:`Rules of Single pointer arithmetic ` * :ref:`Single pointer arithmetic : Increment ` * :ref:`Single pointer arithmetic : Decrement ` * :ref:`Single pointer arithmetic : Division ` * :ref:`Single pointer arithmetic : Multiplication ` .. _basic_ptr_char_sp_ex1_0: .. tab-set:: .. tab-item:: Single pointer and a variable : \*p notation * Step 1 : Define a variable and a pointer .. code-block:: c char x = 65; char *ptr; * Step 2 : Pointer ``ptr`` points to variable ``x`` .. code-block:: c ptr = &x; * Step 3 : Use ``*ptr`` to write to ``x`` .. code-block:: c *ptr = 100; * Step 4 : Use ``*ptr`` to read from ``x`` .. code-block:: c printf("x = %d, *ptr = %d\n", x, *ptr); * See full program below .. code-block:: c #include int main(void) { char x = 65; char *ptr; ptr = &x; printf("x = %d, *ptr = %d\n", x, *ptr); *ptr = 100; printf("x = %d, *ptr = %d\n", x, *ptr); return 0; } * Output is as below .. code-block:: c x = 65, *ptr = 65 x = 100, *ptr = 100 .. _basic_ptr_char_sp_ex1_1: .. tab-set:: .. tab-item:: Single pointer and a variable : p[0] notation * Step 1 : Define a variable and a pointer .. code-block:: c char x = 65; char *ptr; * Step 2 : Pointer ``ptr`` points to variable ``x`` .. code-block:: c ptr = &x; * Step 3 : Use ``ptr[0]`` to write to ``x`` .. code-block:: c ptr[0] = 100; * Step 4 : Use ``ptr[0]`` to read from ``x`` .. code-block:: c printf("x = %d, ptr[0] = %d\n", x, ptr[0] ); * See full program below .. code-block:: c #include int main(void) { char x = 65; char *ptr; ptr = &x; printf("x = %d, ptr[0] = %d\n", x, ptr[0] ); ptr[0] = 100; printf("x = %d, ptr[0] = %d\n", x, ptr[0] ); return 0; } * Output is as below .. code-block:: c x = 65, ptr[0] = 65 x = 100, ptr[0] = 100 .. _basic_ptr_char_sp_ex2: .. tab-set:: .. tab-item:: Single pointer and an array * Step 1 : Define an array and a pointer .. code-block:: c char arr[10] = "Laptop"; char *ptr; * Step 2 : Let Single pointer to point to array .. code-block:: c ptr = arr; OR .. code-block:: c ptr = &arr[0]; * Step 3 : Access individual characters of array using ``ptr[ ]`` notation .. code-block:: c ptr[2] = 'y'; printf("ptr[2] = %c\n", ptr[2] ); Note that, this is same as .. code-block:: c *( ptr + 2 ) = 'y'; printf(" *( ptr + 2 ) = %c\n", *( ptr + 2 ) ); Note that, this is same as .. code-block:: c arr[2] = 'y'; printf("arr[2] = %c\n", arr[2] ); * Step 4 : Access full string and print using ``ptr`` notation .. code-block:: c printf("ptr = %s\n", ptr); Note that, this is same as .. code-block:: c printf("arr = %s\n", arr); * Step 5 : Access full string and modify using ``ptr`` notation .. code-block:: c strcpy(ptr, "Hi"); Note that, this is same as .. code-block:: c strcpy(arr, "Hi"); * See full program below .. code-block:: c #include #include int main(void) { char arr[10] = "Laptop"; char *ptr; ptr = arr; // Change individual character using ptr[] notation ptr[2] = 'y'; // Print individual character using ptr[] notation printf("ptr[2] = %c\n", ptr[2] ); // Print full string using ptr notation printf("ptr = %s\n", ptr); // Change full string using ptr notation strcpy(ptr, "Hi"); // Print full string using ptr notation printf("ptr = %s\n", ptr); return 0; } * Output is as below .. code-block:: c ptr[2] = y ptr = Laytop ptr = Hi .. _basic_ptr_char_sp_ex3: .. tab-set:: .. tab-item:: Single pointer and Heap memory * Step 1 : Define a pointer ``ptr`` .. code-block:: c char *ptr; * Step 2 : Allocate memory in heap and let ``ptr`` point to it .. code-block:: c ptr = malloc(10 * sizeof(char)); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This allocates 10 Bytes of memory in heap * Step 3 : memset and clear the memory before use .. code-block:: c memset(ptr, 0, 10 * sizeof(char) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This is needed because, memory returned by malloc may have garbage contents ! * Step 4 : Access individual characters of heap using ``ptr[ ]`` notation .. code-block:: c ptr[2] = 'y'; printf("ptr[2] = %c\n", ptr[2] ); Note that, this is same as .. code-block:: c *( ptr + 2 ) = 'y'; printf(" *( ptr + 2 ) = %c\n", *( ptr + 2 ) ); * Step 5 : Access full string and print using ``ptr`` notation .. code-block:: c printf("ptr = %s\n", ptr); * Step 6 : Access full string and modify using ``ptr`` notation .. code-block:: c strcpy(ptr, "Hi"); * Step 7 : Free memory after use .. code-block:: c free(ptr); * See full program below .. code-block:: c #include #include #include int main(void) { char *ptr; ptr = malloc(10 * sizeof(char)); memset(ptr, 0, 10 * sizeof(char) ); // Change individual character using ptr[] notation ptr[0] = 'l'; ptr[1] = 'a'; ptr[2] = 'p'; ptr[3] = '\0'; // Print individual character using ptr[] notation printf("ptr[2] = %c\n", ptr[2] ); // Print full string using ptr notation printf("ptr = %s\n", ptr); // Change full string using ptr notation strcpy(ptr, "Hi"); // Print full string using ptr notation printf("ptr = %s\n", ptr); free(ptr); return 0; } * Output is as below .. code-block:: c ptr[2] = p ptr = lap ptr = Hi .. _basic_ptr_char_sp_ex4: .. tab-set:: .. tab-item:: Single pointer and a string * Step 1 : Define a pointer .. code-block:: c char *ptr; * Step 2 : Let ``ptr`` point to a string .. code-block:: c ptr = "Laptop"; * Step 3 : Access individual characters and print .. code-block:: c for (int i = 0; i < strlen(ptr); i++) { printf("%c", ptr[i]); } * Step 4 : Access full string and print .. code-block:: c printf("ptr = %s\n", ptr); * See full program below .. code-block:: c #include #include int main(void) { char *ptr; ptr = "Laptop"; // Access individual characters and print for (int i = 0; i < strlen(ptr); i++) { printf("%c", ptr[i]); } printf("\n"); // Access full string using ptr and print printf("ptr = %s\n", ptr); return 0; } * Output is as below .. code-block:: c Laptop ptr = Laptop .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow In this case content of string "Laptop" can not be changed For example, .. code-block:: c ptr[2] = 'b'; is INVALID ! .. _basic_ptr_char_sp_ex5: .. tab-set:: .. tab-item:: Single pointer and relative memory access * Step 1 : Define an array and a pointer .. code-block:: c char arr[10] = "Laptop"; char *ptr; * Step 2 : Let Single pointer to point to array .. code-block:: c ptr = arr + 2; OR .. code-block:: c ptr = &arr[0] + 2; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow With this ``ptr`` is pointing at Byte 3 (with index 2) * Step 3 : Access Bytes in forward direction .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Observe that, ``ptr`` can access in total of 5 Bytes in forward direction with respect to it's current location ptr[0] is equal to arr[2] ptr[1] is equal to arr[3] ptr[2] is equal to arr[4] ptr[3] is equal to arr[5] ptr[4] is equal to arr[6] * Step 4 : Access Bytes in backward direction .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Observe that, ``ptr`` can access in total of 2 Bytes in backward direction with respect to it's current location ptr[-1] is equal to arr[1] ptr[-2] is equal to arr[0] * See full program below .. code-block:: c #include int main(void) { char arr[10] = "Laptop"; char *ptr; ptr = arr + 2; printf("------ Printing characters using ptr[] notation ------\n"); printf("ptr[-2] = %c\n", ptr[-2]); printf("ptr[-1] = %c\n", ptr[-1]); printf("ptr[0] = %c\n", ptr[0]); printf("ptr[1] = %c\n", ptr[1]); printf("ptr[2] = %c\n", ptr[2]); printf("ptr[3] = %c\n", ptr[3]); printf("ptr[4] = %c\n", ptr[4]); printf("------ Printing characters using *ptr notation ------\n"); printf("*(ptr - 2) = %c\n", *(ptr - 2) ); printf("*(ptr - 1) = %c\n", *(ptr - 1) ); printf("*ptr = %c\n", *ptr ); printf("*(ptr + 1) = %c\n", *(ptr + 1) ); printf("*(ptr + 2) = %c\n", *(ptr + 2) ); printf("*(ptr + 3) = %c\n", *(ptr + 3) ); printf("*(ptr + 4) = %c\n", *(ptr + 4) ); return 0; } * Output is as below .. code-block:: c ------ Printing characters using ptr[] notation ------ ptr[-2] = L ptr[-1] = a ptr[0] = p ptr[1] = t ptr[2] = o ptr[3] = p ptr[4] = ------ Printing characters using *ptr notation ------ *(ptr - 2) = L *(ptr - 1) = a *ptr = p *(ptr + 1) = t *(ptr + 2) = o *(ptr + 3) = p *(ptr + 4) = .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Relative memory access is applicable if pointer is pointing to heap, strings as well ! .. _basic_ptr_char_sp_ex6: .. tab-set:: .. tab-item:: Rules of Single pointer arithmetic .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow =============================================================================== ========== Rules of Single Pointer Arithmetic =============================================================================== ========== * Single Pointer CAN BE Incremented * Incrementing a Single Pointer takes it to next immediate accessible Unit * Single Pointer CAN BE Decremented * Decrementing a Single Pointer takes it to previous immediate accessible Unit * Single Pointer CAN NOT BE used in Division * Single Pointer CAN NOT BE used in Multiplication * Two single pointers CAN BE subtracted * Two single pointers CAN NOT BE added * Two single pointers CAN NOT BE divided * Two single pointers CAN NOT BE multiplied =============================================================================== ========== .. _basic_ptr_char_sp_ex7: .. tab-set:: .. tab-item:: Single pointer arithmetic : Increment * Step 1 : Define an array .. code-block:: c char arr[10] = "Laptop"; * Step 2 : Let ``ptr`` point to array ``arr`` .. code-block:: c char *ptr; ptr = arr; * Step 3 : Increment ``ptr`` twice .. code-block:: c ptr++; ptr++; OR .. code-block:: c ptr += 2; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``ptr`` is now pointing to Byte 2 * See full program below .. code-block:: c #include int main(void) { char arr[10] = "Laptop"; char *ptr; ptr = arr; ptr++; ptr++; // ptr is now at Byte 2 printf("ptr = %s\n", ptr); return 0; } * Output is as below .. code-block:: c ptr = ptop .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow From current location of ``ptr``, %s iterates through all characters till it finds NULL .. _basic_ptr_char_sp_ex8: .. tab-set:: .. tab-item:: Single pointer arithmetic : Decrement * Step 1 : Define an array .. code-block:: c char arr[10] = "Laptop"; * Step 2 : Let ``ptr`` point to array ``arr`` .. code-block:: c char *ptr; ptr = arr + strlen(arr) - 1; * Step 3 : Decrement ``ptr`` twice .. code-block:: c ptr--; ptr--; OR .. code-block:: c ptr -= 2; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``ptr`` is now pointing to Byte 3 * See full program below .. code-block:: c #include int main(void) { char arr[10] = "Laptop"; char *ptr; ptr = arr + strlen(arr) - 1; ptr--; ptr--; // ptr is now at Byte 3 printf("ptr = %s\n", ptr); return 0; } * Output is as below .. code-block:: c ptr = top .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow From current location of ``ptr``, %s iterates through all characters till it finds NULL .. _basic_ptr_char_sp_ex9: .. tab-set:: .. tab-item:: Single pointer arithmetic : Division * Step 1 : Define an array .. code-block:: c char arr[10] = "Laptop"; * Step 2 : Let ``ptr`` point to array ``arr`` .. code-block:: c char *ptr; ptr = arr; * Step 3 : Increment ``ptr`` twice .. code-block:: c ptr = ptr / 2; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This is INVALID * See full program below .. code-block:: c #include int main(void) { char arr[10] = "Laptop"; char *ptr; ptr = arr; ptr = ptr / 2; return 0; } * Output is as below .. code-block:: c p14_char_sp.c: In function main: p14_char_sp.c:11:12: error: invalid operands to binary / (have char * and int) 11 | ptr = ptr / 2; | .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Note the compilation error ! .. _basic_ptr_char_sp_ex10: .. tab-set:: .. tab-item:: Single pointer arithmetic : Multiplication * Step 1 : Define an array .. code-block:: c char arr[10] = "Laptop"; * Step 2 : Let ``ptr`` point to array ``arr`` .. code-block:: c char *ptr; ptr = arr; * Step 3 : Increment ``ptr`` twice .. code-block:: c ptr = ptr * 2; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This is INVALID * See full program below .. code-block:: c #include int main(void) { char arr[10] = "Laptop"; char *ptr; ptr = arr; ptr = ptr * 2; return 0; } * Output is as below .. code-block:: c p14_char_sp.c: In function main: p14_char_sp.c:11:12: error: invalid operands to binary * (have char * and int) 11 | ptr = ptr * 2; | .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Note the compilation error ! .. card:: See Also * Current Module * :doc:`../basic_ptr` * Previous Module * :doc:`../../variable_and_ptr/variable_and_ptr` * Next Module * :doc:`../../array_n_ptrs/array_n_ptrs` * Other Modules * :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:`../../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`