Pointer Arithmetic In C/C++

Share Your Love

On the pointers, we can do arithmetic operations such as addition, subtraction, and so on. However, because the address is stored in the pointer, the result of an arithmetic operation on the pointer will also be a pointer if the other operand is of type integer.

The outcome of pointer-from-pointer subtraction will be an integer value. In the C/C++ programming language, the pointer can perform the following arithmetic operations:

  • Increment
  • Decrement
  • Addition
  • Subtraction
  • Comparison

Incrementing Pointer in C/C++:

If we increase the value of a pointer by one, the pointer will begin pointing to the next closest location. This differs from normal arithmetic in that the size of the data type to which the pointer is referring increases the value of the pointer.

We can traverse an array by incrementing a pointer that will maintain pointing to each element of the array, execute some operation on it, and update itself in a loop.

The following is the rule for incrementing the pointer:

new_address= current_address + i * size_of(data type)  

Where i is the incrementation factor for the pointer.

For 32-Bit Computing System:

For the 32-bit int variable, it will be incremented by 2 bytes.

For 64-Bit Computing System:

For the 64-bit int variable, it will be incremented by 4 bytes.

Let’s look at an example of a pointer variable being incremented on 64-bit architecture in C/C++.

#include<stdio.h>  
int main()
{  
	int n=10;        
	int *p;//pointer to int      
	p=&n;//stores the address of number variable        
	printf("Address of p variable is %u \n",p);        
	p=p+1;        
	printf("After increment: Address of p variable is %u \n",p); // in our case, p will get incremented by 4 bytes.      
	return 0;  
}

Output:

Address of p variable is 1000
After increment: Address of p variable is 1004 
#include <iostream>
using namesapce std;

int main()
{  
	int n=10;        
	int *p;//pointer to int      
	p=&n;//stores the address of number variable        
	cout<<"Address of p variable is "<<p;        
	p=p+1;        
	cout<<"After increment: Address of p variable is"<<p; // in our case, p will get incremented by 4 bytes.      
	return 0;  
}

Output:

Address of p variable is 1000
After increment: Address of p variable is 1004 

Decrementing Pointer in C/C++:

We can decrement a pointer variable in the same way that we may increment it. When we decrement a pointer, it returns to its previous position. The following is the formula for decrementing the pointer:

new_address= current_address - i * size_of(data type)  

For 32-Bit Computing System:

It will be decremented by 2 bytes for 32-bit int variables.

For 64-Bit Computing System:

It will be decremented by 4 bytes for 64-bit int variables.

Let’s look at an example of a 64-bit OS decrementing pointer variable.

#include <stdio.h>            
void main()
{            
	int n=50;        
	int *p;//pointer to int      
	p=&n;//stores the address of number variable        
	printf("Address of p variable is %u \n",p);        
	p=p-1;       
	printf("After decrement: Address of p variable is %u \n",p); // P will now point to the immidiate previous location.         
}  

Output:

Address of p variable is 3214 
After decrement: Address of p variable is 3210

Pointer Addition In C/C++:

We can give the pointer variable a value. The formula for increasing the value of a pointer is as follows:

new_address= current_address + (number * size_of(data type))  

For 32-Bit Computing System:

For the 32-bit int variable, it will add a 2 * number.

For 64-Bit Computing System:

For the 64-bit int variable, it will add a 4 * number.

On a 64-bit architecture, let’s look at an example of adding value to a pointer variable.

#include<stdio.h>  
int main()
{  
	int n=50;        
	int *p;//pointer to int      
	p=&n;//stores the address of number variable        
	printf("Address of p variable is %u \n",p);        
	p=p+3;   //adding 3 to pointer variable    
	printf("After adding 3: Address of p variable is %u \n",p);       
	return 0;  
} 

Output:

Address of p variable is 864300 
After adding 3: Address of p variable is 864312

P’s address is 864300, as you can see. However, after multiplying by 3 with the p variable, the result is 864312, i.e. a 43=12 increment. It increments 12 because we are utilising 64-bit architecture. However, if we used 32-bit architecture, it would only rise to 6, i.e. 23=6. In a 32-bit operating system, an integer value takes up two bytes of memory.

Pointer Subtraction In C/C++:

We can subtract a value from the pointer variable, just like we can add it. Any number subtracted from a pointer yields an address. The following is the formula for removing a value from the pointer variable:

new_address= current_address - (number * size_of(data type))  

For 32-Bit Computing System:

For the 32-bit int variable, it will subtract the 2 * number.

For 64-Bit Computing System:

For the 64-bit int variable, it will subtract the 4 * number.

On a 64-bit architecture, let’s look at an example of subtracting a value from a pointer variable.

#include<stdio.h>  
int main()
{  
	int n=50;        
 	int *p;//pointer to int      
	p=&n;//stores the address of number variable        
	printf("Address of p variable is %u \n",p);        
	p=p-3; //subtracting 3 from pointer variable    
	printf("After subtracting 3: Address of p variable is %u \n",p);        
	return 0;  
}   

Output:

Address of p variable is 3214864300 
After subtracting 3: Address of p variable is 3214864288

After subtracting 3 from the pointer variable, the address value is 12 (4*3) less than the previous value.

We can, however, subtract an address from another address instead of subtracting a number (pointer). A number will be generated as a result of this. It won’t be a straightforward arithmetic operation, but it will comply with the following guidelines.

If two pointers of the same type are compared,

Address2 - Address1 = (Subtraction of two addresses)/size of data type which pointer points

To subtract one pointer from another, consider the following example.

#include<stdio.h>  
void main ()  
{  
    int i = 100;   
    int *p = &i;  
    int *temp;  
    temp = p;   
    p = p + 3;  
    printf("Pointer Subtraction: %d - %d = %d",p, temp, p-temp);  
}

Output:

Pointer Subtraction: 85080 -  85068 = 3

Can a pointer address be negative?

A pointer does not have a sign, but it can be positive or negative when converted to a signed integer. Passing a pointer to printf() with a format variable set to print an integer, by the way, results in undefined behaviour.

Illegal arithmetic with pointers:

There are a number of operations that can’t be done with pointers. Because a pointer contains an address, we must ignore operations that could result in an incorrect address, such as addition and multiplication. The following is a list of such operations.

Address + Address = illegal
Address * Address = illegal
Address % Address = illegal
Address / Address = illegal
Address & Address = illegal
Address ^ Address = illegal
Address | Address = illegal
~Address = illegal

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 411

Newsletter Updates

Enter your email address below to subscribe to our newsletter