catalogue
The C pointer is a numeric address. Therefore, you can perform arithmetic operations on the pointer. Four arithmetic operations can be performed on the pointer: + +, --, +, -.
Suppose , ptr , is an integer pointer to address 1000, which is a 32-bit integer. Let's perform the following arithmetic operations on the pointer:
ptr++
After performing the above operations, ptr # will point to position 1004, because every time ptr increases, it will point to the next integer position, that is, the current position will be moved back 4 bytes. This operation moves the pointer to the next memory location without affecting the actual value in the memory location. If ptr points to a character with an address of 1000, the above operation will cause the pointer to point to position 1001, because the next character position is 1001.
Let's summarize:
- With each increment of the pointer, it actually points to the storage unit of the next element.
- With each decrement of the pointer, it points to the storage unit of the previous element.
- The number of bytes that the pointer jumps when increasing or decreasing depends on the length of the variable data type pointed to by the pointer. For example, int is 4 bytes.
Increment a pointer
We like to use pointers instead of arrays in programs, because variable pointers can be incremented, while arrays cannot be incremented. Arrays can be regarded as pointer constants. The following program increments the variable pointer to sequentially access each element in the array:
#include <stdio.h> const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* Array address in pointer */ ptr = var; for ( i = 0; i < MAX; i++) { printf("Storage address: var[%d] = %p\n", i, ptr ); printf("Stored value: var[%d] = %d\n", i, *ptr ); /* Point to next position */ ptr++; } return 0; }
When the above code is compiled and executed, it will produce the following results:
Storage address: var[0] = e4a298cc Stored value: var[0] = 10 Storage address: var[1] = e4a298d0 Stored value: var[1] = 100 Storage address: var[2] = e4a298d4 Stored value: var[2] = 200
Decrement a pointer
Similarly, decrement the pointer, that is, subtract the number of bytes of its data type from the value, as shown below:
#include <stdio.h> const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* The address of the last element in the pointer */ ptr = &var[MAX-1]; for ( i = MAX; i > 0; i--) { printf("Storage address: var[%d] = %p\n", i-1, ptr ); printf("Stored value: var[%d] = %d\n", i-1, *ptr ); /* Point to next position */ ptr--; } return 0; }
When the above code is compiled and executed, it will produce the following results:
Storage address: var[2] = 518a0ae4 Stored value: var[2] = 200 Storage address: var[1] = 518a0ae0 Stored value: var[1] = 100 Storage address: var[0] = 518a0adc Stored value: var[0] = 10
Pointer comparison
Pointers can be compared with relational operators, such as = =, < and >. If p1 and p2 point to two related variables, such as different elements in the same array, p1 and p2 can be compared in size.
The following program modifies the above instance. As long as the address pointed to by the variable pointer is less than or equal to the address of the last element of the array & var [max - 1], the variable pointer is incremented:
#include <stdio.h> const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* The address of the first element in the pointer */ ptr = var; i = 0; while ( ptr <= &var[MAX - 1] ) { printf("Storage address: var[%d] = %p\n", i, ptr ); printf("Stored value: var[%d] = %d\n", i, *ptr ); /* Point to previous position */ ptr++; i++; } return 0; }
When the above code is compiled and executed, it will produce the following results:
Storage address: var[0] = 0x7ffeee2368cc Stored value: var[0] = 10 Storage address: var[1] = 0x7ffeee2368d0 Stored value: var[1] = 100 Storage address: var[2] = 0x7ffeee2368d4 Stored value: var[2] = 200