Address arithmetic is a method of calculating the address of an object with the help of arithmetic operations on pointers and use of pointers in comparison operations. Address arithmetic is also called pointer arithmetic.
According to C and C++ language standards, the result address must remain strictly within the bounds of a single array object (or just after it). Adding or subtracting from a pointer moves it by a multiple of the size of the data type it points to. For example, assume we have a pointer to an array of 4-byte integers. Incrementing this pointer will increment its value by 4 (the size of the element). This effect is often used to increment a pointer to point at the next element in a contiguous array of integers.
Pointer arithmetic cannot be performed on void pointers because the void type has no size, and thus the pointed address cannot be added to. However, there are non-standard compiler extensions allowing byte arithmetic on void* pointers.
Pointers and integer variables are not interchangeable. Null constant is the only exception from this rule: it can be assigned to the pointer, and the pointer can be compared to null constant. As a rule, to show that null is a special value for the pointer, NULL constant is written instead of number "0".
Pointer arithmetic provides the programmer with a single way of dealing with different types: adding and subtracting the number of elements required instead of the actual offset in bytes. In particular, the C definition explicitly declares that the syntax A[i], which is the i-th element of the array A, is equivalent to *(A+i), which is the content of the element pointed by A+i. This also implies that i[A] is equivalent to A[i].
At the same time, powerful pointer arithmetic can be a source of errors. Address arithmetic is a restrictive stage in the process of mastering 64-bit systems for the old C/C++ program code can contain a lot of errors relating to the use of pointers. To learn more about this problem see the article "20 issues of porting C++ code on the 64-bit platform".
Because of the challenges of using pointers, many modern high level computer languages (for example, Java or C#) do not permit direct access to memory using addresses.
- Brian W. Kernighan, Dennis M. Ritchie - The C Programming Language (2nd Edition). (chapter: 5.4. Address Arithmetic).
- Wikipedia. Pointer (computing). https://en.wikipedia.org/wiki/Pointer_(computer_programming)
- Andrey Karpov, Evgeniy Ryzhkov. 20 issues of porting C++ code on the 64-bit platform. http://www.viva64.com/en/a/0004/
- 64-bit Lessons. Lesson 13. Pattern 5. Address arithmetic.