Pointer Arithmetics in C with Examples
Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn’t store any value.
Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer arithmetic operations are slightly different from the ones that we generally use for mathematical calculations. These operations are:
- Increment/Decrement of a Pointer
- Addition of integer to a pointer
- Subtraction of integer to a pointer
- Subtracting two pointers of the same type
- Comparison of pointers of the same type
1. Increment/Decrement of a Pointer
Increment: It is a condition that also comes under addition. When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer.
If an integer pointer that stores address 1000 is incremented, then it will increment by 4(size of an int), and the new address will point to 1004. While if a float type pointer is incremented then it will increment by 4(size of a float) and the new address will be 1004.
Decrement: It is a condition that also comes under subtraction. When a pointer is decremented, it actually decrements by the number equal to the size of the data type for which it is a pointer.
If an integer pointer that stores address 1000 is decremented, then it will decrement by 4(size of an int), and the new address will point to 996. While if a float type pointer is decremented then it will decrement by 4(size of a float) and the new address will be 996.
Note: It is assumed here that the architecture is 64-bit and all the data types are sized accordingly. For example, integer is of 4 bytes.
Example of Pointer Increment and Decrement
Below is the program to illustrate pointer increment/decrement:
p = 1441900792 p++ = 1441900796 p-- = 1441900792 q = 1441900796 q++ = 1441900800 q-- = 1441900796 r = 1441900791 r++ = 1441900792 r-- = 1441900791
Note: Pointers can be outputted using %p, since, most of the computers store the address value in hexadecimal form using %p gives the value in that form. But for simplicity and understanding we can also use %u to get the value in Unsigned int form.
2. Addition of Integer to Pointer
When a pointer is added with an integer value, the value is first multiplied by the size of the data type and then added to the pointer.
Consider the same example as above where the ptr is an integer pointer that stores 1000 as an address. If we add integer 5 to it using the expression, ptr = ptr + 5, then, the final address stored in the ptr will be ptr = 1000 + sizeof(int) * 5 = 1020.
Example of Addition of Integer to Pointer
Pointer ptr2 before Addition: 0x7ffca373da9c Pointer ptr2 after Addition: 0x7ffca373daa8
3. Subtraction of Integer to Pointer
When a pointer is subtracted with an integer value, the value is first multiplied by the size of the data type and then subtracted from the pointer similar to an addition.
Consider the same example as above where the ptr is an integer pointer that stores 1000 as an address. If we subtract integer 5 from it using the expression, ptr = ptr – 5, then, the final address stored in the ptr will be ptr = 1000 – sizeof(int) * 5 = 980.
Example of Subtraction of Integer from Pointer
Below is the program to illustrate pointer Subtraction:
Pointer ptr2 before Subtraction: 0x7ffd718ffebc Pointer ptr2 after Subtraction: 0x7ffd718ffeb0
4. Subtraction of Two Pointers
The subtraction of two pointers is possible only when they have the same data type. The result is generated by calculating the difference between the addresses of the two pointers and calculating how many bits of data it is according to the pointer data type. The subtraction of two pointers gives the increments between the two pointers.
Two integer pointers say ptr1(address:1000) and ptr2(address:1004) are subtracted. The difference between addresses is 4 bytes. Since the size of int is 4 bytes, therefore the increment between ptr1 and ptr2 is given by (4/4) = 1.
Example of Subtraction of Two Pointer
Below is the implementation to illustrate the Subtraction of Two Pointers:
ptr1 = 2715594428, ptr2 = 2715594424 Subtraction of ptr1 & ptr2 is 1
5. Comparison of pointers of the same type
We can compare the two pointers by using the comparison operators in C. We can implement this by using all operators in C >, >=, <, <=, ==, !=. It returns true for the valid condition and returns false for the unsatisfied condition.
- Step 1: Initialize the integer values and point these integer values to the pointer.
- Step 2: Now, check the condition by using comparison or relational operators on pointer variables.
- Step 3: Display the output.
Example of Pointer Comparision
5 less than 6 6 greater than 5 Both the values are equal Both the values are not equal
Comparison to NULL
A pointer can be compared or assigned a NULL value irrespective of what is the pointer type. Such pointers are called NULL pointers and are used in various pointer-related error-handling methods.
The pointer is NULL
Comparison operators on Pointers using an array
In the below approach, it results in the count of odd numbers and even numbers in an array. We are going to implement this by using a pointer.
- Step 1: First, declare the length of an array and array elements.
- Step 2: Declare the pointer variable and point it to the first element of an array.
- Step 3: Initialize the count_even and count_odd. Iterate the for loop and check the conditions for the number of odd elements and even elements in an array
- Step 4: Increment the pointer location ptr++ to the next element in an array for further iteration.
- Step 5: Print the result.
Example of Pointer Comparison in Array
No of even elements in an array is : 5 No of odd elements in an array is : 5
Pointer Arithmetic on Arrays
Pointers contain addresses. Adding two addresses makes no sense because there is no idea what it would point to. Subtracting two addresses lets you compute the offset between the two addresses. An array name acts like a pointer constant. The value of this pointer constant is the address of the first element.
For Example: if an array is named arr then arr and &arr can be used to reference the array as a pointer.
Below is the program to illustrate the Pointer Arithmetic on arrays:
1 2 3 4 5
1 2 3 4 5 6
Please Login to comment...