Pointer arithmetic - operations with pointers


Operations with pointers:
User can perform below operation with pointers:
Adding an integer to pointer
Subtracting an integer from pointer
Subtracting one pointer from another
Comparing two pointers
Adding an integer to pointer:
Consider ptr as a pointer to the array element arr[i], then ptr++ increments ptr to point to the array element arr[i + 1].  Similarly, if ptr points to the array element arr[i], and j is an integer, then "ptr = ptr + j" makes the pointer "ptr" to point to the array element arr[i + j]. 

  #include <stdio.h>
  int main() {
        int *ptr, i = 0, arr[5] = {10, 20, 30, 40, 50};
        ptr = &arr[i];

        printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
        printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
        ++ptr, i++;

        printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
        printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
        ++ptr, i++;

        printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
        printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
        ptr = ptr + 2;

        printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i + 2, arr[i + 2]);
        printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i + 2, (int)&arr[i + 2]);
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  *ptr: 10 arr[0]: 10 ptr: 0xbfcd1724 &arr[0]: 0xbfcd1724
  *ptr: 20 arr[1]: 20 ptr: 0xbfcd1728 &arr[1]: 0xbfcd1728
  *ptr: 30 arr[2]: 30 ptr: 0xbfcd172c &arr[2]: 0xbfcd172c
  *ptr: 50 arr[4]: 50 ptr: 0xbfcd1734 &arr[4]: 0xbfcd1734

Subtracting an integer from pointer:
Consider ptr as a pointer to the array element arr[i], then ptr-- decrements ptr to point to the array element arr[i - 1].  Similarly, if ptr points to the array element arr[i], and j is an integer, then ptr = ptr - j makes the pointer ptr to point to the array element arr[i -j].

  #include <stdio.h>
  int main() {
        int *ptr, i = 4, arr[5] = {10, 20, 30, 40, 50};
        ptr = &arr[i];

        printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
        printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
        --ptr, i--;

        printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
        printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
        --ptr, i--;

        printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
        printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
        ptr = ptr - 2;

        printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i - 2, arr[i - 2]);
        printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i - 2, (int)&arr[i - 2]);
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  *ptr: 50 arr[4]: 50 ptr: 0xbf9ad2d4 &arr[4]: 0xbf9ad2d4
  *ptr: 40 arr[3]: 40 ptr: 0xbf9ad2d0 &arr[3]: 0xbf9ad2d0
  *ptr: 30 arr[2]: 30 ptr: 0xbf9ad2cc &arr[2]: 0xbf9ad2cc
  *ptr: 10 arr[0]: 10 ptr: 0xbf9ad2c4 &arr[0]: 0xbf9ad2c4

Subtracting one pointer from another:
Subtracting two pointers of same type ptr2 - ptr1 gives us the number of elements between the two pointers ptr1 and ptr2.  Below is an simple program that illustrates subtraction operation on pointers.

  #include <stdio.h>
  int main() {
        int num, *ptr1, *ptr2;
        int arr[5] = {10, 20, 30, 40, 50};

        /* assigning address to pointers */
        ptr1 = &arr[3];
        ptr2 = &arr[0];

        /* subtracting two pointers of same type */
        num = ptr1 - ptr2;

        printf("Number of elements between ptr1 and ptr2 is %d\n", num);
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Number of elements between ptr1 and ptr2 is 3

Comparing two pointers:
Comparison operations can also be performed on pointers.  Consider two pointers ptr1 and ptr2 points to members of same array, then

If ptr1 is equal to ptr2, then both ptr1 and ptr2 points to same element in the given array.
If ptr1 is greater than ptr2, then ptr2 points to the earlier member in the gi

Comments

Popular posts from this blog

Difference between asterisk and ampersand operators in c

kbhit in c