Open In App

C++ Pointer Arithmetic

Last Updated : 21 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, pointer variables are used to store the addresses of other variables, functions, structures, and even other pointers and we can use these pointers to access and manipulate the data stored at that address.

Pointer arithmetic means performing arithmetic operations on pointers. It refers to the operations that are valid to perform on pointers. Following are the arithmetic operations valid on pointers in C++:

  1. Incrementing and Decrementing Pointers
  2. Addition of Constant to Pointers
  3. Subtraction of Constant from Pointers
  4. Subtraction of Two Pointers of the Same Type
  5. Comparison of Pointers

We will explore all the operations listed above in detail with examples.

Prerequisite: To understand pointer arithmetic in C++, we should know Pointers in C++.

1. Incrementing and Decrementing Pointer in C++

Incrementing or decrementing a pointer will make it refer to the address of the next or previous data in the memory. This process differs from incrementing and decrementing numeric data. 

For example, when we increment or decrement a numeric data, its value is incremented or decremented by 1. However, incrementing or decrementing a pointer, instead of increasing or decreasing by 1, the address increases or decreases by 1 multiplied by the size of the data type it is pointing to. (one of the reasons why the pointer declaration requires the information about the type of data it is pointing to)

Incrementing a Pointer

Incrementing a pointer will depend on the type of variable address stored in the pointer. If the pointer stored the address of the integer type variable then the size of the integer pointer can be 4 or 8 bytes depending upon the 32-bit machine or 64-bit machine respectively. So, now if we increment an integer type variable it will be incremented by 4 bytes or 8 bytes depending upon its size.

For example, If a pointer holds the address 1000 and we increment the pointer, then the pointer will be incremented by 4 or 8 bytes (size of the integer), and the pointer will now hold the address 1004.

Decrementing a Pointer

When we apply a decrement operation on the pointer then the pointer is decremented by 4 or 8 bytes depending upon the machine.

For example, If a pointer holds the address 1004 and we decrement the pointer, then the pointer will be decremented by 4 or 8 bytes (size of the integer), and the pointer will now hold the address 1000.

Pointer-Increment-Decrement-in-C++

Example of Pointer Increment and Decrement

The below C++ program demonstrates the increment and decrement of a pointer.

C++




// CPP program to demonstrate the incrementy and decrement
// of a pointer
#include <iostream>
using namespace std;
  
int main()
{
  
    int num = 27;
    // Storing address of num in num_pointer
    int* num_pointer = #
  
    // Print size of int
    cout << "Size of int: " << sizeof(int) << endl;
  
    // Print the address stored at num_pointer
    cout << "Before Increment: " << num_pointer << endl;
    // Increment pointer
    num_pointer++;
  
    cout << "After Increment: " << num_pointer << endl;
  
    // Print the address stored at num_pointer
    cout << "Before Decrement: " << num_pointer << endl;
    // Decrement pointer
    num_pointer--;
  
    cout << "After Decrement: " << num_pointer << endl;
  
    return 0;
}


Output

Size of int: 4
Before Increment: 0x7ffe3e7f56d4
After Increment: 0x7ffe3e7f56d8
Before Decrement: 0x7ffe3e7f56d8
After Decrement: 0x7ffe3e7f56d4

Explanation

We have subtracted ‘1’ from pointer ptr which stored the address of variable ‘num’. While subtraction firstly ‘1’ is multiplied by the size of the integer which is 4 bytes and then subtracted from the pointer.

In the same way, this will be applicable to other data types such as float, double, char, etc.

Refer to the article to learn how to access elements of an array using pointers.

2. Addition of Constant to Pointers

We can add integer values to Pointers and the pointer is adjusted based on the size of the data type it points to. For example, if an integer pointer stores the address 1000 and we add the value 5 to the pointer, it will store the new address as:

1000 + (5 * 4(size of an integer)) = 1020

Pointer-Addition-in-C++

Example

The below C++ code demonstrates the addition of a constant to a pointer.

C++




// CPP program to demonstrate the addition of a constant to
// a pointer
#include <iostream>
using namespace std;
  
int main()
{
  
    int num = 20;
    int* ptr = #
  
    cout << "Address stored in ptr: " << ptr << endl;
  
    // Adding the integer value 1 to the pointer ptr
    ptr = ptr + 1;
    cout << "Adding 1 to ptr: " << ptr << endl;
  
    // Adding the integer value 2 to the pointer ptr
    ptr = ptr + 2;
    cout << "Adding 2 to ptr: " << ptr << endl;
  
    return 0;
}


Output

Address stored in ptr: 0x7ffdb8634a94
Adding 1 to ptr: 0x7ffdb8634a98
Adding 2 to ptr: 0x7ffdb8634aa0

Explanation

We have initialized a variable of integer type and then initialized a pointer ‘ptr’ of integer type with an address of a variable ‘num’. Now, we print the address stored in pointer ‘ptr’ after that we have added ‘1’ to ptr and then again print the address stored in ptr. Now we can see in the output that the ‘4’ is added to ptr instead of ‘1’. This is because when we add any constant to the pointer, it first multiplies it to the size of the type of pointer which is ‘integer’ in this case and here it takes 4 bytes. so, it first multiplies ‘1’ with ‘4’ and then adds the (1*4) to the pointer. The same will happen when we add 2, (2*4) will be added to the pointer ‘ptr’.

3. Subtraction of Constant from Pointers

We can also subtract a constant from Pointers and it is the same as the addition of a constant to a pointer. For example, if an integer pointer stores the address 1000 and we subtract the value 5 from the pointer, it will store the new address as:

1000 - (5 * 4(size of an integer)) = 980

Pointer-Subtraction-in-C++

Example

The below C++ code demonstrates the Subtraction of a constant from a pointer.

C++




// CPP program to demonstrate the subtraction of a constant
// from a pointer
#include <iostream>
using namespace std;
  
int main()
{
  
    int num = 100;
  
    int* ptr = #
  
    cout << "Address stored in ptr: " << ptr << endl;
  
    // Subtracting the integer value 1 from pointer ptr
    ptr = ptr - 1;
    cout << "Subtract 1 from ptr: " << ptr << endl;
  
    return 0;
}


4. Subtraction of Two Pointers of the Same Datatype

The Subtraction of two pointers can be done only when both pointers are of the same data type. The subtraction of two pointers gives the number of elements present between the two pointers.

Example

The below C++ code demonstrates the subtraction of two pointers.

C++




// CPP program to demonstrate the subtraction of two
// pointers
#include <iostream>
using namespace std;
  
int main()
{
  
    int num = 45;
  
    int* ptr1 = #
  
    // Adding 4 to ptr1 and stored in ptr2
    int* ptr2 = ptr1 + 4;
  
    cout << "Address stored in ptr1:" << ptr1 << endl;
    cout << "Address stored in ptr2:" << ptr2 << endl;
  
    // Subtracting ptr2 from ptr1
    cout << "ptr2 - ptr1 = " << ptr2 - ptr1 << endl;
  
    return 0;
}


Output

Address stored in ptr1:0x7ffdd3f5673c
Address stored in ptr2:0x7ffdd3f5674c
ptr2 - ptr1 = 4

5. Comparison of Pointers

In C++, we can perform a comparison between the two pointers using the relational operators(>, <, >=, <=, ==, !=). We generally use this operation to check whether the two-pointer as pointing to the same memory location or not.

Example: Comparing Pointer Variables

C++




// C++ Program to illustrate the pointer comparison
#include <iostream>
using namespace std;
  
int main()
{
    // declaring some pointers
    int num = 10;
    int* ptr1 = #
    int** ptr2 = &ptr1;
    int* ptr3 = *ptr2;
  
    // comparing equality
    if (ptr1 == ptr3) {
        cout << "Both point to same memory location";
    }
    else {
        cout << "ptr1 points to: " << ptr1 << endl;
        cout << "ptr3 points to: " << ptr3 << endl;
    }
    return 0;
}


Output

Both point to same memory location

Comparison to NULL

We can compare the pointer of a type to NULL. This operation helps us to find whether the given pointer points to some memory address or not. It helps us to control errors such as segmentation faults.

Example

C++




// C++ program to compare the pointer to NULL
#include <iostream>
using namespace std;
  
int main()
{
    int num = 10;
    // assigning null in case we dont use pointer
    int* ptr = NULL;
    ptr = #
  
    // checking if the pointer is in use or not
    if (ptr == NULL) {
        cout << "No value is pointed";
    }
    else {
        cout << "The value pointed is " << *ptr;
    }
    return 0;
}


Output

The value pointed is 10

Note: It is recommend that we should initialize new pointer variables with NULL so that we can make check if the pointer is assigned some meaningful value.

Related Articles



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads