C++ Pointer Arithmetic
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: ++, --, +, -.
Supposeptris an integer pointer pointing to address 1000, a 32-bit integer. Let's perform the following arithmetic operations on this pointer:
ptr++
After executing ptr++, the pointer ptr will move forward by 4 bytes, pointing to the address of the next integer element. This is because pointer arithmetic determines the distance moved 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.
ifptrIt points to a character at address 1000. When ptr++ is executed, the value of the pointer ptr will increase, 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 analysis of pointer arithmetic operations:
Addition operation: You can perform addition on pointers. When an integer n is added to a pointer p, the result is that pointer p moves forward by the size of n elements. For example, if p is a pointer to int and each int occupies 4 bytes, then p + 1 will point to the next int element after the one p points to.
Subtraction operation: You can perform subtraction on pointers. When an integer n is subtracted from a pointer p, the result is that pointer p moves backward by the size of n elements. For example, if p is a pointer to int and each int occupies 4 bytes, then p - 1 will point to the previous int element before the one p points to.
Subtraction between pointers: You can calculate the distance between two pointers. When one pointer q is subtracted from another pointer p, the result is the number of elements between the two pointers. For example, if p and q are both pointers to int and each int occupies 4 bytes, then p - q will yield the number of elements between the two pointers.
Comparison operation between a pointer and an integer: You can compare a pointer with an integer. You can use relational operators (such as <, >, <=, >=) to compare pointers with integers. This comparison is usually used to determine whether a pointer points to a valid memory location.
Increment a pointer
In C++, a pointer is a variable that stores a memory address. Incrementing a pointer means making the pointer point to the next memory location, which usually means pointing to the next array element. Incrementing a pointer automatically adjusts the pointer's value according to the data type the pointer points to.
For example, if a pointer points to an element of an int array, then incrementing the pointer will make it point to the next int element. Here is a simple example demonstrating how to increment a pointer:
Example
When the above code is compiled and executed, it produces the following results:
指针当前指向的元素: 10 递增指针后指向的元素: 20
In this example, the pointerptrInitially points to the arrayarrthe first element. By executingptr++, the pointerptris incremented to point to the next element of the array (i.e., the second element).
When incrementing a pointer, the pointer's value will increase by an offset equal to the size of the data type the pointer points to. For example, if the pointer is of typeint*type, each increment of the pointer will increase it by 4 bytes (assuminginttype occupies 4 bytes).
It should be noted that when using pointer operations, you must ensure that the pointer points to a valid memory region; otherwise, it may cause undefined behavior or program crashes. When operating on arrays, be especially careful to avoid pointers going beyond the bounds of the array.
Decrement a pointer
In C++, pointers can not only be incremented but also decremented. Decrementing a pointer means making the pointer point to the previous memory location. Similar to incrementing a pointer, decrementing a pointer also automatically adjusts the pointer's value according to the data type the pointer points to.
Here is a simple example demonstrating how to decrement a pointer:
Example
When the above code is compiled and executed, it produces the following results:
指针当前指向的元素: 20 递减指针后指向的元素: 10
In this example, the pointerptrInitially points to the arrayarrthe second element. By executingptr--, the pointerptris decremented to point to the first element of the array.
When decrementing a pointer, the pointer's value will decrease by an offset equal to the size of the data type the pointer points to. For example, if the pointer is of typeint*type, each decrement of the pointer will decrease it by 4 bytes (assuminginttype occupies 4 bytes).
Pointer comparison
In C++, pointer comparison operations can be used to determine whether two pointers point to the same location, whether one pointer points to a location before or after another pointer's location, etc. Pointer comparison mainly includes the following:
- Equality comparison (
==and!=) - Relational comparison (
<,<=,>,>=)
Equality comparison
Equality comparison is used to check whether two pointers point to the same location.
Example
int main() {
int a = 10;
int b = 20;
int* ptr1 = &a;
int* ptr2 = &a;
int* ptr3 = &b;
// Compare whether the pointers are equal
if (ptr1 == ptr2) {
std::cout << ptr1 and ptr2 point to the same location << std::endl;
} else {
std::cout << ptr1 and ptr2 point to different locations << std::endl;
}
if (ptr1 != ptr3) {
std::cout << ptr1 and ptr3 point to different locations << std::endl;
} else {
std::cout << ptr1 and ptr3 point to the same location << std::endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following results:
ptr1 和 ptr2 指向相同的位置 ptr1 和 ptr3 指向不同的位置
Relational comparison
Relational comparison is used to determine whether one pointer points to a location before or after another pointer's location. This is usually meaningful when pointers point to elements of the same array.
Example
int main() {
int arr[] = {10, 20, 30, 40, 50};
int* ptr1 = &arr[1]; // Point to the second element of the array
int* ptr2 = &arr[3]; // Point to the fourth element of the array
// Compare the relative positions of the pointers
if (ptr1 < ptr2) {
std::cout << The element pointed to by ptr1 is before the element pointed to by ptr2 << std::endl;
} else {
std::cout << The element pointed to by ptr1 is not before the element pointed to by ptr2 << std::endl;
}
if (ptr2 > ptr1) {
std::cout << The element pointed to by ptr2 is after the element pointed to by ptr1 << std::endl;
} else {
std::cout << The element pointed to by ptr2 is not after the element pointed to by ptr1 << std::endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following results:
ptr1 指向的元素在 ptr2 指向的元素之前 ptr2 指向的元素在 ptr1 指向的元素之后
Complete example of pointer comparison:
Example
int main() {
int arr[] = {10, 20, 30, 40, 50};
int* ptr1 = &arr[1]; // Point to the second element of the array
int* ptr2 = &arr[3]; // Point to the fourth element of the array
int* ptr3 = &arr[1]; // Point to the second element of the array
// Equality comparison
if (ptr1 == ptr3) {
std::cout << ptr1 and ptr3 point to the same location << std::endl;
} else {
std::cout << ptr1 and ptr3 point to different locations << std::endl;
}
if (ptr1 != ptr2) {
std::cout << ptr1 and ptr2 point to different locations << std::endl;
} else {
std::cout << ptr1 and ptr2 point to the same location << std::endl;
}
// Relational comparison
if (ptr1 < ptr2) {
std::cout << The element pointed to by ptr1 is before the element pointed to by ptr2 << std::endl;
} else {
std::cout << The element pointed to by ptr1 is not before the element pointed to by ptr2 << std::endl;
}
if (ptr2 > ptr1) {
std::cout << The element pointed to by ptr2 is after the element pointed to by ptr1 << std::endl;
} else {
std::cout << The element pointed to by ptr2 is not after the element pointed to by ptr1 << std::endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following results:
ptr1 和 ptr3 指向相同的位置 ptr1 和 ptr2 指向不同的位置 ptr1 指向的元素在 ptr2 指向的元素之前 ptr2 指向的元素在 ptr1 指向的元素之后
Notes
Comparisons within the same array range: Relational comparisons (such as
<,>,<=,>=) between elements of the same array are meaningful. If the pointers do not belong to the same array, the result of the relational comparison is undefined.Pointer is null: Before comparing pointers, ensure that the pointers are not null pointers (
nullptr), otherwise it may cause undefined behavior.