Main Page | See live article | Alphabetical index

Pointer arithmetic

Pointer arithmetic is a particular arithmetic involving pointers, typical of the C programming language.

In pointer arithmetic, the unit is the size of the pointer's type. For example, adding 1 to a pointer to integer values (which typically span 4 bytes) will increment the pointer by 4. But, the end result will be in the pointer pointing right at the next element, which was probably the intended result.

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[n], which is the n-th element of the array pointed by a, is equivalent to *(a+n), which is the content of the element pointed by a+n.

While powerful, pointer arithmetic is an endless source of computer bugs. It tends to confuse programmers, forcing them into different contexts: an expression can be an ordinary arithmetic one or a pointer arithmetic one, and sometimes it is easy to mistake one for the other.

Many modern high level computer languages (for example Java) do not permit direct access to memory using addresses, so concepts of pointers and pointer arithmetic are not relevant. This is often deliberate, as many programming tasks do not require specific knowledge of where and how in computer memory data is stored.