In C++, arithmetic operations on pointer variable is similar to a numeric value. As we know that, a pointer in C++ is a variable used to store the memory address which is a numeric value. The arithmetic operations on pointer variable changes the memory address pointed by pointer. Not all arithmetic operations are valid for pointer variable, like multiplication and division. Here is the list of valid pointer arithmetic operations.
- Incrementing a pointer.
- Decrementing a pointer.
- Adding a number to pointer.
- Subtracting a number form a pointer.
- Comparison on two pointers.
- Subtracting two pointers.
- Addition of two pointers.
- Division of two pointers.
- Multiplication of two pointers.
Adding a numbers to a Pointer
Addition of a number N to a pointer leads the pointer to a new memory location after skipping (N x size of pointer data type) bytes.
ptr + N = ptr + (N * sizeof(pointer_data_type))
For example, Let ptr be an integer pointer, initially pointing to location 1000. The size of integer data type is 4 bytes. Then ptr + 5 = 1000 + 4*5 = 1020. Pointer ptr will now point at memory address 1020.
Subtracting Numbers from Pointers
Subtraction of a number N from a pointers is similar to adding a number except in subtraction the new location will be before current location by (N x size of pointer data type).
ptr - N = ptr - (N * sizeof(pointer_data_ype))
For example, Let ptr be a double pointer, initially pointing to location 1000. The size of double data type is 6 bytes. Then ptr - 3 = 1000 - 6*3 = 982. Pointer ptr will now point at memory address 982.
Subtraction of Two Pointers
The difference between two pointer returns indicates “How apart the two Pointers are. It gives the total number of elements between two pointers. For example, Let size of integer is 4 bytes. If an integer pointer 'ptr1' points at memory location 10000 and integer pointer 'ptr' points at memory location 10008, the result of ptr2 - ptr1 is 2.
Incrementing a Pointer in C++
We use increment operator(++) to increment a pointer. The effect of incrementing an integer and an integer pointer is different. Let ptr be an integer pointer which points to the memory location 5000 and size of an integer variable is 4 bytes. Now, when we increment pointer ptr
ptr++;
it will point to memory location 5004 because it will jump to the next integer location which is 4 bytes next to the current location. Incrementing a pointer is not same as incrementing an integer value. On incrementing, a pointer will point to the memory location after skipping N bytes, where N is the size of the data type(in this case it is 4).
ptr++ is equivalent to ptr + (sizeof(pointer_data_type)).
"Incrementing a pointer increases its value by the number of bytes of its data type".
- An integer(4 bytes) pointer on increment jumps 4 bytes.
- A character(1 bytes) pointer on increment jumps 1 bytes.
Decrementing a Pointer in C++
We use decrement operator(--) to decrement a pointer. Decrementing a pointer will decrease its value by the number of bytes of its data type. Let ptr is originally pointing to memory location 5000. Hence, after
ptr--;ptr will point to 4996.
ptr--; is equivalent to ptr - (sizeof(pointer_data_type)).