C Pointer Arithmetic
A C 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: ++, --, +, -.
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 performing the above operations,ptr will point to location 1004, because each time ptr is incremented, it points to the next integer location, that is, 4 bytes forward from the current location. This operation moves the pointer to the next memory location without affecting the actual value at the memory location. If ptr points to a character at address 1000, the above operation will cause the pointer to point to location 1001, because the next character location is at 1001.
Let's summarize:
- Each time a pointer is incremented, it actually points to the storage unit of the next element.
- Each time a pointer is decremented, it points to the storage unit of the previous element.
- The number of bytes a pointer jumps when incremented or decremented depends on the size of the data type of the variable the pointer points to; for example, int is 4 bytes.
Incrementing a Pointer
Incrementing a pointer means making the pointer point to the next memory location.
The increment operation of a pointer performs an appropriate memory offset based on the data type the pointer points to.
We prefer to use pointers instead of arrays in programs, because a pointer variable can be incremented while an array cannot; an array can be regarded as a pointer constant. The following program increments a pointer variable to access each element of the array in sequence:
Example
#include
<
stdio.h
const
int
MAX
=
3;
int
main
(
)
{
// Define an integer array
int
var
[
]
=
{
10
,
100
,
200
}
;
// Define an integer variable i and an integer pointer ptr
int
i
, *
ptr
;
// Point the pointer ptr to the starting address of the array var
ptr
=
var
;
// Loop through the array
for
(
i
=
0
;
i
<
MAX
;
i
++
)
{
// Print the address currently pointed to by the pointer ptr
printf
(
"
Storage address: var[%d] = %p
\
n
"
,
i
,
ptr
)
;
// Print the value at the address currently pointed to by the pointer ptr
printf
(
"
Stored value: var[%d] = %d
\
n
"
,
i
, *
ptr
)
;
// Move the pointer ptr to the position of the next array element
ptr
++;
}
return
0
;
}
When the code above is compiled and executed, it produces the following result:
存储地址:var[0] = e4a298cc
存储值:var[0] = 10
存储地址:var[1] = e4a298d0
存储值:var[1] = 100
存储地址:var[2] = e4a298d4
存储值:var[2] = 200
Incrementing a character pointer:
Example
#include
int
main
(
)
{
char
str
[
]
=
"Hello"
;
char
ptr
=
str
;
// The pointer points to the first character of the string
printf
(
"Initial character: %c\n"
,
ptr
)
;
// Output H
ptr
++;
// Increment the pointer so that it points to the next character
printf
(
"Character after increment: %c\n"
,
ptr
)
;
// Output e
return
0
;
}
In this example, ptr++ makes the pointer move from str[0] to str[1]. Because ptr is a char pointer, when it is incremented it moves sizeof(char) bytes, that is, 1 byte.
When the code above is compiled and executed, it produces the following result:
初始字符: H
递增后字符: e
Incrementing a structure pointer:
Example
#include
struct
Point
{
int
x
;
int
y
;
}
;
int
main
(
)
{
struct
Point points
[
]
=
{
{
1
,
2
}
,
{
3
,
4
}
,
{
5
,
6
}
}
;
struct
Point
ptr
=
points
;
// The pointer points to the first element of the structure array
printf
(
"Initial point: (%d, %d)\n"
,
ptr
->
x
,
ptr
->
y
)
;
// Output (1, 2)
ptr
++;
// Increment the pointer so that it points to the next structure
printf
(
"Point after increment: (%d, %d)\n"
,
ptr
->
x
,
ptr
->
y
)
;
// Output (3, 4)
return
0
;
}
In this example, ptr++ makes the pointer move from points[0] to points[1]. Because ptr is a struct Point pointer, when it is incremented it moves sizeof(struct Point) bytes.
When the code above is compiled and executed, it produces the following result:
初始点: (1, 2)
递增后点: (3, 4)
Decrementing a Pointer
Decrementing a pointer means making the pointer point to the previous memory location. Similar to incrementing a pointer, the decrement operation of a pointer also performs an appropriate memory offset based on the data type the pointer points to.
Performing a decrement operation on a pointer means subtracting the number of bytes of its data type from the value, as shown below:
Example
#include
<
stdio.h
int
main
(
)
{
int
arr
[
]
=
{
10
,
20
,
30
,
40
,
50
};
int
ptr
= &
arr
[
4
]
;
// The pointer points to the last element of the array
printf
(
"
Initial value: %d
\
n
"
, *
ptr
)
;
// Output 50
ptr
--;
// Decrement the pointer so that it points to the previous integer element
printf
(
"
Value after decrement: %d
\
n
"
, *
ptr
)
;
// Output 40
ptr
--;
// Decrement the pointer again
printf
(
"
Value after second decrement: %d
\
n
"
, *
ptr
)
;
// Output 30
return
0
;
}
When the code above is compiled and executed, it produces the following result:
初始值: 50
递减后值: 40
再次递减后值: 30
Decrementing a character pointer:
Example
#include
int
main
(
)
{
char
str
[
]
=
"Hello"
;
char
ptr
=
&
str
[
4
]
;
// The pointer points to the last character 'o' of the string
printf
(
"Initial character: %c\n"
,
ptr
)
;
// Output o
ptr
--;
// Decrement the pointer so that it points to the previous character
printf
(
"Character after decrement: %c\n"
,
ptr
)
;
// Output l
ptr
--;
// Decrement the pointer again
printf
(
"Character after second decrement: %c\n"
,
ptr
)
;
// Output l
return
0
;
}
When the code above is compiled and executed, it produces the following result:
初始字符: o
递减后字符: l
再次递减后字符: l
Decrementing a structure pointer:
Example
#include
struct
Point
{
int
x
;
int
y
;
}
;
int
main
(
)
{
struct
Point points
[
]
=
{
{
1
,
2
}
,
{
3
,
4
}
,
{
5
,
6
}
}
;
struct
Point
ptr
=
&
points
[
2
]
;
// The pointer points to the last element of the structure array
printf
(
"Initial point: (%d, %d)\n"
,
ptr
->
x
,
ptr
->
y
)
;
// Output (5, 6)
ptr
--;
// Decrement the pointer so that it points to the previous structure
printf
(
"Point after decrement: (%d, %d)\n"
,
ptr
->
x
,
ptr
->
y
)
;
// Output (3, 4)
ptr
--;
// Decrement the pointer again
printf
(
"Point after second decrement: (%d, %d)\n"
,
ptr
->
x
,
ptr
->
y
)
;
// Output (1, 2)
return
0
;
}
When the code above is compiled and executed, it produces the following result:
初始点: (5, 6)
递减后点: (3, 4)
再次递减后点: (1, 2)
Pointer Comparison
In C, pointers can be compared to determine their relationship. Pointer comparison is mainly used to determine whether two pointers point to the same memory location or whether one pointer is before or after another.
Pointers can be compared using relational operators, such as==, !=, <, >, <= and >=. If p1 and p2 point to two related variables, such as different elements in the same array, then p1 and p2 can be compared in terms of size.
Here are some example codes showing how to compare pointers and output the results.
Pointer equality comparison:
Example
#include
<
stdio.h
int
main
(
)
{
int
a
=
5;
int
b
=
10
;
int
ptr1
= &
a
;
int
ptr2
= &
a
;
int
ptr3
= &
b
;
if
(
ptr1
==
ptr2
)
{
printf
(
"
ptr1 and ptr2 point to the same memory address
\
n
"
)
;
// This line will be output
}
else
{
printf
(
"
ptr1 and ptr2 point to different memory addresses
\
n
"
)
;
}
if
(
ptr1
!=
ptr3
)
{
printf
(
"
ptr1 and ptr3 point to different memory addresses
\
n
"
)
;
// This line will be output
}
else
{
printf
(
"
ptr1 and ptr3 point to the same memory address
\
n
"
)
;
}
return
0
;
}
When the code above is compiled and executed, it produces the following result:
ptr1 和 ptr2 指向相同的内存地址
ptr1 和 ptr3 指向不同的内存地址
Pointer magnitude comparison:
Example
#include
int
main
(
)
{
int
arr
[
]
=
{
10
,
20
,
30
,
40
,
50
}
;
int
ptr1
=
&
arr
[
1
]
;
// Points to arr[1], with a value of 20
int
ptr2
=
&
arr
[
3
]
;
// Points to arr[3], with a value of 40
if
(
ptr1
<
ptr2
)
{
printf
("ptr1 is before ptr2\n"
)
;
// This line will be output
}
else
{
printf
(
"ptr1 is after ptr2 or at the same position\n"
)
;
}
if
(
ptr1
ptr2
)
{
printf
(
"ptr1 is after ptr2\n"
)
;
}
else
{
printf
(
"ptr1 is before ptr2 or at the same position\n"
)
;
// This line will be output
}
return
0
;
}
When the code above is compiled and executed, it produces the following result:
ptr1 在 ptr2 之前
ptr1 在 ptr2 之前或相同位置
Iterate through the array and compare pointers:
Example
#include
int
main
(
)
{
int
arr
[
]
=
{
10
,
20
,
30
,
40
,
50
}
;
int
start
=
arr
;
// Points to the first element of the array
int
end
=
&
arr
[
4
]
;
// Points to the last element of the array
int
ptr
;
for
(
ptr
=
start
;
ptr
<=
end
;
ptr
++
)
{
printf
(
"Value pointed to by the current pointer: %d\n"
,
ptr
)
;
}
return
0
;
}
When the code above is compiled and executed, it produces the following result:
当前指针指向的值: 10
当前指针指向的值: 20
当前指针指向的值: 30
当前指针指向的值: 40
当前指针指向的值: 50
Summary
- Equality comparison (
==and!=)): used to determine whether two pointers point to the same memory location. - Magnitude comparison (
<,>,<=,>=)): generally used when traversing arrays or memory blocks with pointers, to determine whether one pointer is before or after another.
Note that pointer comparison is only meaningful when the pointers point to the same array or the same memory block; otherwise the behavior is undefined.
C Pointer