Cpp Pointer Arithmetic
[ C++ Pointer](#)
A pointer is an address represented by a numeric value. Therefore, you can perform arithmetic operations on pointers. Four arithmetic operations can be performed on pointers: ++, --, +, and -.
Assume **ptr** is an integer pointer pointing to address 1000, which is a 32-bit integer. Let us perform the following arithmetic operations on this pointer:
ptr++
After executing ptr++, the pointer ptr moves forward by 4 bytes, pointing to the address of the next integer element. This is because pointer arithmetic determines the distance to move based on the type and size of the pointer. In this case, since it is a 32-bit integer pointer, each integer occupies 4 bytes, so ptr++ will move the pointer ptr forward by 4 bytes, pointing to the address of the next integer element.
If **ptr** points to a character at address 1000, executing ptr++ will increase the value of pointer ptr, pointing to the address of the next character element. Since ptr is a character pointer and each character occupies 1 byte, ptr++ will increase the value of ptr by 1, and after execution, ptr points to address 1001.
Detailed explanation of pointer arithmetic:
* **Addition operation**: You can perform addition on pointers. When a pointer p is added to an integer n, the result is that pointer p moves forward by n element sizes. For example, if p is an int pointer and each int occupies 4 bytes, then p + 1 will point to the next int element after p.
* **Subtraction operation**: You can perform subtraction on pointers. When a pointer p is subtracted by an integer n, the result is that pointer p moves backward by n element sizes. For example, if p is an int pointer and each int occupies 4 bytes, then p - 1 will point to the previous int element before p.
* **Subtraction between pointers**: You can calculate the distance between two pointers. When subtracting another pointer q from a pointer p, the result is the number of elements between the two pointers. For example, if p and q are two int pointers and each int occupies 4 bytes, then p - q will give the number of elements between the two pointers.
* **Comparison between pointers and integers**: You can compare pointers with integers. You can use relational operators (such as , =) to compare pointers and integers. This type of comparison is typically used to determine whether a pointer points to a valid memory location.
## Incrementing a Pointer
In C++, a pointer is a variable that stores a memory address. Incrementing a pointer means pointing to the next memory location, which is usually the next array element. Incrementing a pointer automatically adjusts the pointer's value based on the data type it points to.
For example, if a pointer points to an element of an int array, incrementing the pointer will make it point to the next int element. Below is a simple example demonstrating how to increment a pointer:
## Example
#includeint main(){int arr[] = {10, 20, 30, 40, 50}; int* ptr = arr; std::cout<<"PointerElement currently pointed to: "<< *ptr<<std::endl; ptr++; std::cout<<"Element Pointed to After Incrementing Pointer: "<< *ptr<<std::endl; return 0; }
When the above code is compiled and executed, it produces the following result:
PointerElement currently pointed to: 10Element Pointed to After Incrementing Pointer: 20
In this example, the pointer `ptr` initially points to the first element of the array `arr`. By executing `ptr++`, the pointer `ptr` is incremented to point to the next element in the array (the second element).
When incrementing a pointer, the pointer's value increases by an offset equal to the size of the data type the pointer points to. For example, if the pointer is of type `int*`, each increment will increase by 4 bytes (assuming the `int` type occupies 4 bytes).
It should be noted that when using pointer operations, ensure that the pointer points to a valid memory area, otherwise it may lead to undefined behavior or program crashes. When operating on arrays, special care should be taken to avoid the pointer going out of the array's bounds.
## Decrementing a Pointer
In C++, pointers can not only be incremented but also decremented. Decrementing a pointer means pointing to the previous memory location. Similar to incrementing a pointer, decrementing a pointer also automatically adjusts the pointer's value based on the data type it points to.
Below is a simple example demonstrating how to decrement a pointer:
## Example
#includeint main(){int arr[] = {10, 20, 30, 40, 50}; int* ptr = &arr; std::cout<<"PointerElement currently pointed to: "<< *ptr<<std::endl; ptr--; std::cout<<"Element pointed to after decrementing the pointer: "<< *ptr<<std::endl; return 0; }
When the above code is compiled and executed, it produces the following result:
PointerElement currently pointed to: 20Element pointed to after decrementing the pointer: 10
In this example, the pointer `ptr` initially points to the second element of the array `arr`. By executing `ptr--`, the pointer `ptr` is decremented to point to the first element of the array.
When decrementing a pointer, the
YouTip