Skip to main content

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 given array.
  • If ptr1 is lesser than ptr2, then ptr1 points to the earlier member in the given array.

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

        /* assigning same address to pointer ptr1 and ptr2 */
        ptr1 = ptr2 = &arr[0];

        if (ptr1 == ptr2) {
                printf("Condition 1:\n");
                printf("ptr1 and ptr2 points to same element of the array\n");
                printf("ptr1: 0x%x\t*ptr1: %d\n", (int)ptr1, *ptr1);
                printf("ptr2: 0x%x\t*ptr2: %d\n", (int)ptr2, *ptr2);
        }

        /* advancing pointer ptr1 */
        ptr1 = ptr1 + 2;

        if (ptr1 > ptr2) {
                printf("\nCondition 2:\n");
                printf("ptr2 points to earlier element in the array\n");
                printf("ptr1: 0x%x\t*ptr1: %d\n", (int)ptr1, *ptr1);
                printf("ptr2: 0x%x\t*ptr2: %d\n", (int)ptr2, *ptr2);
        }

        /* advancing pointer ptr2 */
        ptr2 = ptr2 + 4;

        if (ptr2 > ptr1) {
                printf("\nCondition 3:\n");
                printf("ptr1 points to earlier element in the array\n");
                printf("ptr1: 0x%x\t*ptr1: %d\n", (int)ptr1, *ptr1);
                printf("ptr2: 0x%x\t*ptr2: %d\n", (int)ptr2, *ptr2);
        }

        return 0;
  }



  Output:
  Condition 1:
  ptr1 and ptr2 points to same element of the array
  ptr1: 0xbf83c374 *ptr1: 10
  ptr2: 0xbf83c374 *ptr2: 10

  Condition 2:
  ptr2 points to earlier element in the array
  ptr1: 0xbf83c37c *ptr1: 30
  ptr2: 0xbf83c374 *ptr2: 10

  Condition 3:
  ptr1 points to earlier element in the array
  ptr1: 0xbf83c37c *ptr1: 30
  ptr2: 0xbf83c384 *ptr2: 50



Can we add two pointers in C?
No, addition of two pointers is not allowed.


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

        /* adding two pointers */
        ptr1 = ptr1 + ptr2;
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ gcc addPtr.c 
  addPtr.c: In function ‘main’:
  addPtr.c:7: error: invalid operands to binary + (have ‘int *’ and ‘int *’)


Comments

Popular posts from this blog

scanf() and printf() functions

Scanf() function: It is used to read input from the keyboard. int scanf(const char *format, ...); For better understanding, lets rewrite the above as follows. scanf("format specifiers", &v1, &v2, &v3); v1, v2 and v3 are the input variables whose value needs to read from the keyboard.  We need to pass address of the variable as argument to store the input values from keyboard. Here, ampersand(&) represents address. &v1 - address of variable v1 Input value for v1 will be stored at address of variable v1 Format specifier can be any of the following %u  - unsigned int %d  - int %x  - hexadecimal %o  - octal %f  - float %lf - double %c  - char %s  - string scanf("%d", &v1); - inputs integer value scanf("%x", &v2); - inputs hexadecimal value scanf("%f", &v3); - inputs float value printf() function: It prints the given data on the output screen. printf("format specifiers", v1, v2,..vn); Here, format specifier ...

How to install JDK and setting environment variables for java

In this tutorial, we are going to see the following. How to install JDK How to run java program without setting java path How to set temporary java path in windows How to set permanent java path in windows How to install JDK? Check whether JDK is installed on your PC.  If it is not installed, please go to the below link and download JDK under Java SE(Java Standard Edition) in oracle website . Install the downloaded JDK in your PC. Once JDK is installed, you can run your java program using any method shown below. Having your java source code under jdk/bin, compile the source code and run the program from same directory Setting temporary path for java, compiling the source code and running the program from any location using command prompt Updating the java path in system or user variable(in system properties), compiling the source code and running the program anywhere using command prompt Let us see how to run java program without setting environmental variables.  Once java is ...

Multidimensional arrays in C

Multidimensional array is also known as array of arrays.  C allows user to write arrays with one or more dimensions.  An array with more than one dimension is a multi-dimensional array.  Two dimensional array is a simplest form of multidimensional array.  Below is the general form of multidimensional array. data_type  array_name[size1][size2][size3]..[sizeN]; Consider the following example int arr[2][2][2] = {   { {1, 1}, {1, 1} },                                   { {1, 1}, {1, 1} }                               } ;   The above statement shows the declaration and initialization of multidimensional array(3d array).  Total number of elements in an array can be obtained by multiplying the size value in each dimension. int arr[2][2][2]; 2 X 2 X 2 = 8 elements in the array arr And multidim...