C++ Pointer Arithmetic (original) (raw)

In C++, pointer arithmetic means performing valid arithmetic operations on pointer variables to move and access memory locations efficiently.

The following diagram assumes size of integer as 4 bytes.

Pointer-Increment-Decrement-in-C++

C++ `

#include using namespace std;

int main() {

int n = 27;
int* ptr = &n;

cout << "Size of int: " << sizeof(int) << endl;
cout << "Before Increment: " << ptr << endl;

ptr++;
cout << "After Increment: " << ptr << endl;
cout << "Before Decrement: " << ptr << endl;

ptr--;
cout << "After Decrement: " << ptr;
return 0;

}

`

Output

Size of int: 4 Before Increment: 0x7ffcbc721cec After Increment: 0x7ffcbc721cf0 Before Decrement: 0x7ffcbc721cf0 After Decrement: 0x7ffcbc721cec

**Explanation: We have subtracted '1' from pointer ptr which stored the address of variable 'n'. 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.

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 ptr stores the address 1000 and we add the value 5 to the pointer.

ptr + 5

It will calculate the new address as:

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

Pointer-Addition-in-C++

C++ `

#include using namespace std;

int main(){

int n = 20;
int* ptr = &n;

cout << "Address stored in ptr: " << ptr << endl;
ptr = ptr + 1;
cout << "Adding 1 to ptr: " << ptr << endl;

ptr = ptr + 2;
cout << "Adding 2 to ptr: " << ptr;
return 0;

}

`

Output

Address stored in ptr: 0x7ffc79d0fcec Adding 1 to ptr: 0x7ffc79d0fcf0 Adding 2 to ptr: 0x7ffc79d0fcf8

**Explanation:

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 ptr stores the address 1000 and we subtract the value 5 from the pointer:

ptr - 5

It will calculate the new address as:

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

Pointer-Subtraction-in-C++

C++ `

#include using namespace std;

int main(){

int n = 100;
int* ptr = &n;

cout << "Address stored in ptr: " << ptr << endl;
ptr = ptr - 1;
cout << "Subtract 1 from ptr: " << ptr;
return 0;

}

`

Output

Address stored in ptr: 0x7ffdd556d7cc Subtract 1 from ptr: 0x7ffdd556d7c8

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 memory addresses so it is primarily valid if the two pointers belong to the same array.

For Example, in an array arr[10], ptr1 point points to the element at index 2 and ptr2 points to the element at index 4, then the difference between two pointers will give you the number of elements between them.

ptr2 - ptr1

Example

C++ `

#include using namespace std;

int main() { int arr[5] = {1, 2, 3, 4, 5};

int* ptr1 = &arr[2];
int* ptr2 = &arr[4];

cout << ptr2 - ptr1;
return 0;

}

`

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.

C++ `

#include using namespace std;

int main() { int n = 10; int* ptr1 = &n; 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;
}
return 0;

}

`

Output

Both point to same memory location

**Explanation:

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.

C++ `

#include using namespace std;

int main() { int n = 10; int* ptr = NULL; ptr = &n;

if (ptr == NULL) {
    cout << "No value is pointed";
}
else {
    cout << *ptr;
}
return 0;

}

`

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