C++ Pointer Arithmetic
A pointer is an address represented by a numeric value. Therefore, you can perform arithmetic operations on pointers. There are four arithmetic operations that can be performed on pointers: ++, --, +, -.
Assume ptr is an integer pointer pointing to address 1000 and is a 32-bit integer; let us perform the following arithmetic operations on this pointer:
ptr++
After executing ptr++, the pointer ptr moves forward 4 bytes to point 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 and each integer occupies 4 bytes, ptr++ moves the pointer ptr forward by 4 bytes to point to the address of the next integer element.
If both the ptr points to a character at address 1000; after executing ptr++, the value of pointer ptr increases to point to the address of the next character element. Since ptr is a character pointer and each character occupies 1 byte, ptr++ increments the value of ptr by 1, so after execution ptr points to address 1001.
Detailed analysis of pointer arithmetic:
-
Addition: You can perform addition on a pointer. When a pointer p is added to an integer n, the result is that pointer p moves forward by the size of n elements. For example, if p is a pointer of type int and each int occupies 4 bytes, then p + 1 will point to the next int element that p points to.
-
Subtraction: You can perform subtraction on a pointer. When a pointer p is subtracted by an integer n, the result is that pointer p moves backward by the size of n elements. For example, if p is a pointer of type int and each int occupies 4 bytes, then p - 1 will point to the previous int element that p points to.
-
Subtraction between pointers: You can compute the distance between two pointers. When one pointer p is subtracted from another pointer q, the result is the number of elements between the two pointers. For example, if p and q are two pointers of type int and each int occupies 4 bytes, then p - q gives the number of elements between the two pointers.
-
Comparison between a pointer and an integer: a pointer can be compared with an integer. You can use relational operators (such as<, >, <=, >=) to compare a pointer with an integer. Such comparisons are usually 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 moving the pointer to the next memory location, which is usually the next array element. Incrementing a pointer automatically adjusts the value of the pointer based on the data type it points to.
For example, if the pointer points to an array element of type int, then incrementing the pointer will make it point to the next int element. The following is a simple example demonstrating how to increment a pointer:
Example
#include
<
iostream
int
main
(
)
{// Define an array
int
arr
[
]
=
{
10
,
20
,
30
,
40
,
50
}
;
// Define a pointer to the first element of the array
int
ptr
=
arr
;
// Output the element the pointer points to
std
::
cout
<<
"
Element currently pointed to by the pointer:
"
<< *
ptr
<<
std
::
endl
;
// Increment the pointer
ptr
++;
// Output the element the pointer points to
std
::
cout
<<
"
Element pointed to after incrementing the pointer:
"
<< *
ptr
<<
std
::
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
指针当前指向的元素: 10
递增指针后指向的元素: 20
In this example, the pointer ptr initially points to the array arr 's first element. By executing ptr++, the pointer ptr is incremented to point to the next element of the array (that is, the second element).
When incrementing a pointer, the value of the pointer increases by an offset equal to the size of the data type the pointer points to. For example, if the pointer is int* type, each increment of the pointer adds 4 bytes (assuming int type occupies 4 bytes).
It should be noted that when using pointer operations, you must ensure that the pointer points to a valid memory area; otherwise, it may lead to undefined behavior or program crashes. When operating on arrays, be especially careful to avoid the pointer going out of the array's bounds.
Decrementing a Pointer
In C++, a pointer can be decremented as well as incremented. Decrementing a pointer means moving the pointer to the previous memory location. Similar to incrementing a pointer, decrementing a pointer also automatically adjusts the value of the pointer based on the data type it points to.
The following is a simple example demonstrating how to decrement a pointer:
Example
#include
<
iostream
int
main
(
)
{// Define an array
int
arr
[
]
=
{
10
,
20
,
30
,
40
,
50
}
;
// Define a pointer to the second element of the array
int
ptr
= &
arr
[
1
]
;
// Output the element the pointer currently points to
std
::
cout
<<
"
Element currently pointed to by the pointer:
"
<< *
ptr
<<
std
::
endl
;
// Decrement the pointer
ptr
--;
// Output the element the pointer points to after decrementing
std
::
cout
<<
"
Element pointed to after decrementing the pointer:
"
<< *
ptr
<<
std
::
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
指针当前指向的元素: 20
递减指针后指向的元素: 10
In this example, the pointer ptr initially points to the array arr 's second element. By executing ptr--, the pointer ptr , it is decremented to point to the first element of the array.
When decrementing a pointer, the value of the pointer decreases by an offset equal to the size of the data type the pointer points to. For example, if the pointer is int* type, each decrement of the pointer reduces it by 4 bytes (assuming int type occupies 4 bytes).
Pointer Comparison
In C++, pointer comparison operations can be used to determine whether two pointers point to the same location, or whether one pointer points to a location before or after another pointer. Pointer comparisons mainly include the following:
- Equality comparison (
==and!=) - Relational comparison (
<,<=,>,>=)
Equality comparison
Equality comparison is used to check whether two pointers point to the same location.
Example
#include
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 code above is compiled and executed, it produces the following result:
ptr1 和 ptr2 指向相同的位置
ptr1 和 ptr3 指向不同的位置
Relational comparison
Relational comparison is used to determine whether one pointer points to a location before or after another pointer. This is usually meaningful when the pointers point to elements of the same array.
Example
#include
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 code above is compiled and executed, it produces the following result:
ptr1 指向的元素在 ptr2 指向的元素之前
ptr2 指向的元素在 ptr1 指向的元素之后
A complete example of pointer comparison:
Example
#include
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 code above is compiled and executed, it produces the following result:
ptr1 和 ptr3 指向相同的位置
ptr1 和 ptr2 指向不同的位置
ptr1 指向的元素在 ptr2 指向的元素之前
ptr2 指向的元素在 ptr1 指向的元素之后
Notes
-
Comparison within the same array range: Relational comparison (such as
<,>,<=,>=) between elements of the same array is 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 the pointer is not a null pointer (
nullptr); otherwise, it may lead to undefined behavior.
C++ Pointers