﻿# Address arithmetic

Address arithmetic is a method of calculating an object address using arithmetic operations on pointers, as well as using pointers in comparison operations\. Address arithmetic is also known as pointer arithmetic\.

According to the C and C\+\+ standards, in pointer arithmetic, the resulting address should remain strictly on the boundary of a single array object \(or follow it immediately\)\. Adding or subtracting a pointer shifts it by a multiple of the data type size it points to\. For example, there is a pointer to an integer array \(4 bytes per element\)\. An increment of the pointer increases its value by 4 \(element size\)\. Programmers often do it to get the Nth element in a sequence container\.

```cpp
int array[10] {};
int *ptr  = array;   // points to the first element
int *next = ptr + 1; // points to the second element
....
int *next = ptr + 9; // points to the last element
```

Pointer arithmetic cannot be applied to pointers to [incomplete](https://pvs-studio.com/en/blog/terms/6528/) types, because their size is unknown\. The compiler does not know how many bytes the pointer should be shifted by\.

```cpp
struct SomeType;          // Incomplete type
....
SomeType* ptr  = ....;
SomeType* next = ptr + 1; // ERROR: 'SomeType *': unknown size
```

However, there are non\-standard compiler extensions that enable you to perform byte arithmetic on untyped pointers \(_void \*_\)\.

Pointers and integer variables are not interchangeable objects\. The 0 constant is the only exception: it can be assigned to a pointer comparable to a null constant\. To show that null is a special value for a pointer, the constant 'NULL' is usually written instead of 0\. Since C23 / C\+\+11, it is better to use [_nullptr_](https://en.cppreference.com/w/cpp/language/nullptr) for this purpose\.

Address arithmetic enables a programmer to work with different types in the same way: by adding and subtracting the required number of elements instead of actually moving bytes\. The C language description explicitly specifies the equivalence of the _A\[i\]_ syntactic structure\. It is the _i_\-th element of the _A_ array, and _\*\(A \+ i\)_ is the content of the element pointed to by the _\(A \+ i\)_ expression\. It is also assumed that _i\[A\]_ is equal to _A\[i\]_\. In other words, all four pointers in the example below point to the same element, just in different ways:

```cpp
int *a = array + 2;
int *b = 2 + array;
int *c = array[2];
int *d = 2[array];
```

Address arithmetic is a fundamental concept of C and C\+\+\. This is why it's often employed when working with arrays, strings, and dynamically allocated memory\. When using address arithmetic, one should be careful\. Make sure the calculations are correct to avoid [array index out of bounds](https://pvs-studio.com/en/blog/terms/0071/) or [access violation](https://pvs-studio.com/en/blog/terms/0063/)\.

Due to the complexity of using pointers, many modern high\-level programming languages \(e\.g\., Java or C\#\) do not allow direct memory access using addresses\.

**Sources**

1. [Wikipedia\. Pointer \(computing\)](https://en.wikipedia.org/wiki/Pointer_(computer_programming))
1. [A Collection of Examples of 64\-bit Errors in Real Programs](https://pvs-studio.com/en/blog/posts/cpp/a0065/#ID81562B564C:~:text=Mbyte%20by%20default.-,Example%2013.%20A%20function%20with%20a%20variable%20number%20of%20arguments%20and%20buffer%20overflow,-Although%20it%20is)
1. [A course on developing 64\-bit C/C\+\+ applications\. Lesson 13\. Pattern 05\. Address arithmetic](https://pvs-studio.com/en/blog/lessons/0013/)