Having covered the basics about pointers in last article, let’s look into pointer arithmetic. The arithmetic operations that are allowed with pointers are adding/subtracting integer to/from a pointer and subtracting two pointers.

**How to add/subtract integer to/from pointer?**

The result of adding/subtracting an integer (‘n’) to/from a pointer will be the address of the nth element (of the same type as pointer) with address in pointer used as a base address.

Let’s look into this with an example. Say we have an integer (32-bit integer) pointer pointing to address 1000. Now if we want to add integer 4 to this address, what the result will be?

So, we will assume address 1000 as the base address into which we will be adding integer, 4. Now, since the pointer is an integer pointer, it means at address 1000 an integer will be stored, since it’s a 32-bit integer, it will take 4 bytes. Hence, starting from address 4 byte will be used by that integer (address 1000, 1001, 1002 & 1003). So, next integer will be stored only starting from address 1004, and taking 4 bytes.

By the above explanation we can see that since every integer takes 4 bytes, going to next valid integer, we need to increment the address by 4 bytes or 4n bytes to reach nth integer. Now, this number 4 has come from the fact that the pointer’s type was integer pointer. Had it was a character pointer, we would have to increment address by 1 byte to reach the next valid character.

So, we can form the following generic formula to find the result of adding an integer to an address.

Result = Pointer + Integer * sizeof(Value stored at Pointer Address).

Let’s apply this to our example:

Pointer = 1000

Integer = 4

Value stored at Pointer Address = integer (as it is a integer pointer)

Sizeof(Value stored at Pointer Address) = 4 (it is a 32-bit integer)

Result = 1000 + 4 * 4 = 1016.

If we replace addition with subtraction in the above text, we can come up with the following formula to subtract an integer from a pointer.

Result = Pointer – Integer * sizeof(Value stored at Pointer Address).

So what is the type of the value that we get as a result of adding/subtracting integer to/from pointer? The answer is, same type as the pointer which was involved in addition/subtraction operation, i.e., Integer pointer, in the above example.

How to subtract two pointers?

Two pointers of same or different types can be subtracted from each other. Though, normally we subtract pointers of same type only.

In short, subtracting two pointers of same type tells us how many elements of the type of pointers can be stored between those two addresses. In case we subtract two pointers of different types, the result is number of elements, of largest type (type that needs more memory) of pointer, that can be stored between two addresses.

The type of value returned by subtracting two pointers is of type integer, as it gives us the element count.

Let’s subtract two pointers of the same type. Say the two pointers are pA and pB with addresses 2000 and 1000 and their types are integer pointers. So, if we do pB – pA. The result should be number of integers that can be stored between 2000 and 1000. Since the memory locations between 2000 and 1000 are 1000 and each integer takes 4 bytes (32-bit integer). Number of integers that can fit in 1000bytes will be 1000/4 = 250. So, if we subtract two integer pointers with addresses 2000 and 1000, the result will be 25.

Now, the above can be put in a generic formula as follows.

Result = (PointerA – PointerB)/sizeof(Value stored at PointerA or PointerB).

Now, let’s look at the case where the two pointers are of different types, say pA is character pointer and pB is short integer pointer. Since size of a character is 1byte and size of short integer is 2bytes, the result of subtraction will tell us how many short integers can be stored in between the two pointers getting subtracted. Since there are 1000 memory locations between two pointers, the result will be 1000/2 = 500.

Now, the above can be put in a generic formula as follows.

Result = (PointerA – PointerB)/MAX(sizeof(Value stored at PointerA), sizeof(Value stored at PointerB)).

Similar topics

Pointer in C/C++ Part – 1