Join Regular Classroom : Visit ClassroomTech

Addition and Subtraction of a Pointer (Pointer Arithmetics) in C

A pointer is an address, means it is a numerical value. Hence, arithmetic operation can be done on a pointer. There are four operations that can be done on a pointer. Those are:

1. Increment (++) and Decrement (–)
2. Addition of any integer to pointer (+)
3. Subtraction of any integer from pointer (-)
4. Subtracting two pointers of same datatype (-)

In this lesson we will only learn about Addition and Subtraction method.

It is inappropriate to add or substract a random integer to a pointer. The arithmetic operation on a pointer is applicable only when a pointer argument refers to an array. When a pointer arithmetic is being performed, the size of the value to add or substract depends upon the datatype of the reference array object the pointer is handling.

The adding or substracting an integer is invalid because it may point to an element address which may not be available within that array. After the arithmetic operation on a pointer the result will also be a pointer if the operand is of integer type.

Below, there is an example for adding some integer to an integer pointer.

Example 1

/* C program to perform addition of pointer */
/* www.codewindow.in */

#include <stdio.h>  

// Driver code
int main(){  
	int a=5;        
	int *ptr;	// Integer type pointer ptr      
	ptr=&a;		//Stores the address of 'a' integer type variable        
	printf("Before addition Address of the variable is %u", ptr);		// %u is to print unsigned integer        
	ptr=ptr+3;      // We add 3 to ptr
	
	printf("\nAfter addition address of the variable is: %u", ptr);
	
    return 0;  
}    

Output

Before addition Address of the variable is 3861401900
After addition address of the variable is: 3861401912

Explanation
In the above example after adding 3 to the ptr pointer the address of ptr will be increased by 12 as for addition of 1 will increment the address by 4 bytes as it is handling a integer type variable of size 4 bytes in a 64 bit machine. So, 3*4=12 bytes will be added to the previous address.

Now let us see an example of subtracting an integer from an integer pointer

Example 2

/* C program to perform subtraction of pointer */
/* www.codewindow.in */

#include <stdio.h>  

// Driver code
int main(){  
	int a=5;        
	int *ptr;	// Integer type pointer ptr      
	ptr=&a;		//Stores the address of 'a' integer type variable        
	printf("Before subtraction Address of the variable is %u", ptr);		// %u is to print unsigned integer        
	ptr=ptr-4;      // We add 3 to ptr
	
	printf("\nAfter subtraction address of the variable is: %u", ptr);
	
    return 0;  
}    

Output

Before subtraction Address of the variable is 126788940
After subtraction address of the variable is: 126788924

Explanation
In the above example after subtracting 4 from the ptr pointer the address of ptr will be decreased by 16 as for subtraction of 1 will decrease the address by 4 bytes as it is handling a integer type variable of size 4 bytes in a 64 bit machine. So, 4*4=16 bytes will be subtracted from the previous address.


NOTE: The your output address may not match with us even with each execution it can change. But it is guaranteed that the difference between both addresses will be same with us (if you use 64 bit machine)

You Missed

Also Checkout