In this tutorial, we will learn about the various operation that can be performed on Pointers. But before that if you do not know about the Pointer click the link below.
As the operation can be performed on numeric value and pointer is an address which is an numeric value so we can perform some operation on Pointers. There are specifically four arithmetic operators that can be performed on the Pointers and those are:
- Increment
++
- Decrement
- -
- Addition
+
- Substraction
-
- Comparision
==
Let us assume that a pointer (ptr) is pointing to some integer variable whose address is 2000. Now let us perform increment operation on that pointer ptr.
ptr++
Now where the ptr points to? Well the ptr address value which was initially 2000 increases to 2004 because of the integer. The integer occupies 4 bytes of storage and so ptr increases by 4. The increment depends on the data-types that you are pointing through your pointer. If your pointer points to character type then the address which was initially at 2000 will increases to 2001 (as the size of the character is 1 byte).
Increment of Pointer in C
As from the above explanation, we conclude that the increment of the pointer by 1 means the pointer value will increase by the size of the data-types that the pointer is pointing.
It is mostly used during the traversing of an array through which the program can reach every element present in an array.
Example for incrementing pointer variable in C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> int main() { int num = 10; int *ptr;//integer pointer //storing the address of num variable ptr = # printf("Address initially at ptr: %p\n", ptr); //pointer increment ptr = ptr + 1; printf("After increment the value of ptr: %p", ptr); return 0; } |
Output: Check the last two digit which is increased by 4 byte (int).
1 2 | Address initially at ptr: 0x7ffc86bcc234 After increment the value of ptr: 0x7ffc86bcc238 |
Example of Traversing an array by incrementing pointer variable in C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> int main () { //declaration of variables int arr[4] = {10, 20, 30, 40}; int i, *ptr; ptr = arr; printf("Printing all the Element in arr.\n"); for(i = 0; i < 4; i++) { printf("arr[%d] = %d\n", i, *ptr ); ptr++; } return 0; } |
Output: Following result is produced after compilation and execution.
1 2 3 4 5 | Printing all the Element in arr. arr[0] = 10 arr[1] = 20 arr[2] = 30 arr[3] = 40 |
Decrement of Pointer in C
Decrement is as same as the increment. We can decrement the value of the pointer depending on its data types the pointer is pointing.
Example for decrementing pointer variable in C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> int main() { int num = 10; int *ptr;//integer pointer //storing the address of num variable ptr = # printf("Address initially at ptr: %p\n", ptr); //pointer decrement ptr = ptr - 1; printf("After decrement the value of ptr: %p", ptr); return 0; } |
Output: Check the last two digit which is decreased by 4 byte (int).
1 2 | Address initially at ptr: 0x7ffd246dc634 After decrement the value of ptr: 0x7ffd246dc630 |
Addition and Subtraction of Pointers in C
We can add or subtract the value of the pointer variable. If you add 2 that means the value of the pointer increases 2 times depending on the size of data types that the pointer pointing to such as,
present_address + (number × size_of_the_data_type)
Subtraction is also processed in the same manner instead of increases 2 times, pointer will be decreased by 2 times the size of the data type.
Example for addition and subtraction of pointer variable in C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<stdio.h> int main() { int num = 10; int *ptr;//integer pointer //storing the address of num variable ptr = # printf("Address initially at ptr: %p\n", ptr); //3 pointer variable added ptr = ptr + 3; printf("After Addition the value of ptr: %p\n", ptr); //3 pointer variable subtracted ptr = ptr - 3; printf("After Subtracting the value of ptr: %p", ptr); return 0; } |
Output: Check the last two digit change.
1 2 3 | Address initially at ptr: 0x7ffc41a0bb42 After Addition the value of ptr: 0x7ffc41a0bb54 After Subtracting the value of ptr: 0x7ffc41a0bb42 |
Pointers Comparision
Pointers can be compared using ==, <, and > operators in C if they are pointing to the same array.
Example of comparing the pointers in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> int main() { int *ptr1, *ptr2; int a = 10, b = 20; ptr1 = &a; ptr2 = &b; if (*ptr1 > *ptr2) //comparing values { printf("ptr1 is greater."); } else { printf("ptr2 is greater."); } return (0); } |
Output: *ptr points to the actual value stored in the address that the pointer points to.
1 | ptr2 is greater. |
Operation that can’t be operated on Pointer:
The following operators are not allowed on the pointer address value as it gives the false address value. You can say that they are illegal operations on Pointers.
- pointer_address + pointer_address
- pointer_address * pointer_address
- pointer_address % pointer_address
- pointer_address / pointer_address
- pointer_address & pointer_address
- pointer_address ^ pointer_address
- pointer_address | pointer_address
- ~pointer_address