Basics of Integer 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 integers ? .. 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 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_int_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 int x = 65; int *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) { int x = 65; int *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_int_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 int x = 65; int *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) { int x = 65; int *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_int_sp_ex2: .. tab-set:: .. tab-item:: Single pointer and an array * Step 1 : Define an array and a pointer .. code-block:: c int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *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 integers of array using ``ptr[ ]`` notation .. code-block:: c ptr[2] = 100; printf("ptr[2] = %d\n", ptr[2] ); Note that, this is same as .. code-block:: c *( ptr + 2 ) = 100; printf(" *( ptr + 2 ) = %d\n", *( ptr + 2 ) ); Note that, this is same as .. code-block:: c arr[2] = 100; printf("arr[2] = %d\n", arr[2] ); * Step 4 : Print full array using ``ptr`` .. code-block:: c for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) { printf("ptr[%d] = %d\n", i, ptr[i]); } * See full program below .. code-block:: c #include #include int main(void) { int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *ptr; ptr = arr; // Change individual integer using ptr[] notation ptr[2] = 100; // Print individual integer using ptr[] notation printf("ptr[2] = %d\n", ptr[2] ); // Print full array using "ptr" for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) { printf("ptr[%d] = %d\n", i, ptr[i]); } return 0; } * Output is as below .. code-block:: c ptr[2] = 100 ptr[0] = 1 ptr[1] = 2 ptr[2] = 100 ptr[3] = 4 ptr[4] = 5 ptr[5] = 6 ptr[6] = 7 ptr[7] = 8 ptr[8] = 9 ptr[9] = 10 .. _basic_ptr_int_sp_ex3: .. tab-set:: .. tab-item:: Single pointer and Heap memory * Step 1 : Define a pointer ``ptr`` .. code-block:: c int *ptr; * Step 2 : Allocate memory in heap and let ``ptr`` point to it .. code-block:: c ptr = malloc(10 * sizeof(int)); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This allocates 40 Bytes of memory in heap * Step 3 : memset and clear the memory before use .. code-block:: c memset(ptr, 0, 10 * sizeof(int) ); .. 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 integers of heap using ``ptr[ ]`` notation .. code-block:: c ptr[2] = 100; printf("ptr[2] = %d\n", ptr[2] ); Note that, this is same as .. code-block:: c *( ptr + 2 ) = 100; printf(" *( ptr + 2 ) = %d\n", *( ptr + 2 ) ); * Step 5 : Free memory after use .. code-block:: c free(ptr); * See full program below .. code-block:: c #include #include #include int main(void) { int *ptr; ptr = malloc(10 * sizeof(int)); memset(ptr, 0, 10 * sizeof(int) ); // Change individual integer using ptr[] notation ptr[0] = 0; ptr[1] = 1; ptr[2] = 2; ptr[3] = 3; ptr[4] = 4; ptr[5] = 5; ptr[6] = 6; ptr[7] = 7; ptr[8] = 8; ptr[9] = 9; ptr[2] = 200; // or *( ptr + 2 ) = 200; ptr[3] = 100; // or *( ptr + 3 ) = 300; // Print individual integer using ptr[] notation printf("ptr[2] = %d\n", ptr[2] ); // Print full array for (int i = 0; i < 10 ; i++) { printf("ptr[%d] = %d\n", i, ptr[i]); } free(ptr); return 0; } * Output is as below .. code-block:: c ptr[2] = 200 ptr[0] = 0 ptr[1] = 1 ptr[2] = 200 ptr[3] = 100 ptr[4] = 4 ptr[5] = 5 ptr[6] = 6 ptr[7] = 7 ptr[8] = 8 ptr[9] = 9 .. _basic_ptr_int_sp_ex5: .. tab-set:: .. tab-item:: Single pointer and relative memory access * Step 1 : Define an array and a pointer .. code-block:: c int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *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 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 8 indexes 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] ptr[5] is equal to arr[7] ptr[6] is equal to arr[8] ptr[7] is equal to arr[9] * 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 indexes 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) { int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *ptr; ptr = arr + 2; printf("------ Printing integers using ptr[] notation ------\n"); printf("ptr[-2] = %d\n", ptr[-2]); printf("ptr[-1] = %d\n", ptr[-1]); printf("ptr[0] = %d\n", ptr[0]); printf("ptr[1] = %d\n", ptr[1]); printf("ptr[2] = %d\n", ptr[2]); printf("ptr[3] = %d\n", ptr[3]); printf("ptr[4] = %d\n", ptr[4]); printf("------ Printing integers using *ptr notation ------\n"); printf("*(ptr - 2) = %d\n", *(ptr - 2) ); printf("*(ptr - 1) = %d\n", *(ptr - 1) ); printf("*ptr = %d\n", *ptr ); printf("*(ptr + 1) = %d\n", *(ptr + 1) ); printf("*(ptr + 2) = %d\n", *(ptr + 2) ); printf("*(ptr + 3) = %d\n", *(ptr + 3) ); printf("*(ptr + 4) = %d\n", *(ptr + 4) ); return 0; } * Output is as below .. code-block:: c ------ Printing integers using ptr[] notation ------ ptr[-2] = 1 ptr[-1] = 2 ptr[0] = 3 ptr[1] = 4 ptr[2] = 5 ptr[3] = 6 ptr[4] = 7 ------ Printing integers using *ptr notation ------ *(ptr - 2) = 1 *(ptr - 1) = 2 *ptr = 3 *(ptr + 1) = 4 *(ptr + 2) = 5 *(ptr + 3) = 6 *(ptr + 4) = 7 .. 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_int_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_int_sp_ex7: .. tab-set:: .. tab-item:: Single pointer arithmetic : Increment * Step 1 : Define an array .. code-block:: c int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; * Step 2 : Let ``ptr`` point to array ``arr`` .. code-block:: c int *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 Index 2 * See full program below .. code-block:: c #include int main(void) { int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *ptr; ptr = arr; ptr++; ptr++; // ptr is now at Index 2 printf("ptr[0] = %d\n", ptr[0]); return 0; } * Output is as below .. code-block:: c ptr[0] = 3 .. _basic_ptr_int_sp_ex8: .. tab-set:: .. tab-item:: Single pointer arithmetic : Decrement * Step 1 : Define an array .. code-block:: c int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; * Step 2 : Let ``ptr`` point to array ``arr`` .. code-block:: c int *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 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 Index 7 * See full program below .. code-block:: c #include int main(void) { int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; ptr--; ptr--; // ptr is now at Index 7 printf("ptr[0] = %d\n", ptr[0]); return 0; } * Output is as below .. code-block:: c ptr[0] = 8 .. _basic_ptr_int_sp_ex9: .. tab-set:: .. tab-item:: Single pointer arithmetic : Division * Step 1 : Define an array .. code-block:: c int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; * Step 2 : Let ``ptr`` point to array ``arr`` .. code-block:: c int *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) { int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *ptr; ptr = arr; ptr = ptr / 2; return 0; } * Output is as below .. code-block:: c p9_int_sp.c: In function "main": p9_int_sp.c:11:19: error: invalid operands to binary / (have "int *" 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_int_sp_ex10: .. tab-set:: .. tab-item:: Single pointer arithmetic : Multiplication * Step 1 : Define an array .. code-block:: c int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; * Step 2 : Let ``ptr`` point to array ``arr`` .. code-block:: c int *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) { int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int *ptr; ptr = arr; ptr = ptr * 2; return 0; } * Output is as below .. code-block:: c p10_int_sp.c: In function "main": p10_int_sp.c:11:19: error: invalid operands to binary * (have "int *" 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`