C Pointer Arithmetic
. Since ptr is a char type pointer, it moves sizeof(char) bytes when incremented, i.e., 1 byte.
SupposeptrIt is an integer pointer pointing to address 1000, and it is a 32-bit integer. Let's perform the following arithmetic operation on this pointer:
ptr++
After performing the above operations,ptrIt will point to location 1004, because each time ptr is incremented, it will point to the next integer location, that is, 4 bytes forward from the current position. This operation moves the pointer to the next memory location without affecting the actual value in the memory location. IfptrIf it points to a character at address 1000, the above operation will cause the pointer to point to location 1001, because the next character position is at 1001.
Let's summarize:
- Each increment of a pointer actually makes it point to the storage unit of the next element.
- Each decrement of a pointer makes it point to the storage unit of the previous element.
- The number of bytes a pointer jumps when incremented or decremented depends on the data type length of the variable it points to; for example, int is 4 bytes.
Increment a pointer
Incrementing a pointer means making the pointer point to the next memory location.
The increment operation on 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 variable pointer can be incremented, while an array cannot be incremented; an array can be viewed as a pointer constant. The following program increments the variable pointer to sequentially access each element in the array:
Example
When the above code is compiled and executed, it produces the following results:
存储地址:var[0] = e4a298cc 存储值:var[0] = 10 存储地址:var[1] = e4a298d0 存储值:var[1] = 100 存储地址:var[2] = e4a298d4 存储值:var[2] = 200
Increment character pointer:
Example
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 to point 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
When the above code is compiled and executed, it produces the following results:
初始字符: H 递增后字符: e
Incrementing a structure pointer:
Example
struct Point {
int x;
int y;
};
int main() {
struct Point points[] = {{1, 2}, {3, 4}, {5, 6}};
struct Point *ptr = points; // Pointer points to the first element of the struct array
printf("Initial point: (%d, %d)\n", ptr->x, ptr->y); // Output (1, 2)
ptr++; // Increment the pointer to point to the next struct
printf("Incremented point: (%d, %d)\n", ptr->x, ptr->y); // Output (3, 4)
return 0;
}
. Because ptr is a pointer of type struct Point, incrementing it advances by sizeof(struct Point) bytes.
When the above code is compiled and executed, it produces the following results:
初始点: (1, 2) 递增后点: (3, 4)
Decrement a pointer
Decrementing a pointer means making the pointer point to the previous memory location. Similar to incrementing a pointer, the decrement operation 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 byte size of its data type from the value, as shown below:
Example
When the above code is compiled and executed, it produces the following results:
初始值: 50 递减后值: 40 再次递减后值: 30
Decrement character pointer:
Example
int main() {
char str[] = "Hello";
char *ptr = &str[4]; // Pointer points to the last character 'o' of the string
printf("Initial character: %c\n", *ptr); // Output o
ptr--; // Decrement the pointer to point to the previous character
printf("Decremented character: %c\n", *ptr); // Output l
ptr--; // Decrement the pointer again
printf("Decremented character again: %c\n", *ptr); // Output l
return 0;
}
When the above code is compiled and executed, it produces the following results:
初始字符: o 递减后字符: l 再次递减后字符: l
Decrementing a structure pointer:
Example
struct Point {
int x;
int y;
};
int main() {
struct Point points[] = {{1, 2}, {3, 4}, {5, 6}};
struct Point *ptr = &points[2]; // Pointer points to the last element of the struct array
printf("Initial point: (%d, %d)\n", ptr->x, ptr->y); // Output (5, 6)
ptr--; // Decrement the pointer to point to the previous struct
printf("Decremented point: (%d, %d)\n", ptr->x, ptr->y); // Output (3, 4)
ptr--; // Decrement the pointer again
printf("Decremented point again: (%d, %d)\n", ptr->x, ptr->y); // Output (1, 2)
return 0;
}
When the above code is compiled and executed, it produces the following results:
初始点: (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 to determine whether one pointer is before or after another pointer.
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 for size.
The following are some example codes that show how to compare pointers and output the results.
Pointer equality comparison:
Example
When the above code is compiled and executed, it produces the following results:
ptr1 和 ptr2 指向相同的内存地址 ptr1 和 ptr3 指向不同的内存地址
Pointer size comparison:
Example
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr1 = &arr[1]; // Points to arr[1], value is 20
int *ptr2 = &arr[3]; // Points to arr[3], value is 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 above code is compiled and executed, it produces the following results:
ptr1 在 ptr2 之前 ptr1 在 ptr2 之前或相同位置
Iterating through an array and comparing pointers:
Example
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("The value pointed to by the current pointer: %d\n", *ptr);
}
return 0;
}
When the above code is compiled and executed, it produces the following results:
当前指针指向的值: 10 当前指针指向的值: 20 当前指针指向的值: 30 当前指针指向的值: 40 当前指针指向的值: 50
Summary
- Equality comparison (
==and!=): Used to determine whether two pointers point to the same memory location. - Size comparison (
<,>,<=,>=): Usually used when pointers traverse arrays or memory blocks, to determine whether one pointer is before or after another pointer.