Pointer Subtraction Issue
In C, the result of subtracting two pointers represents the number of elements between the two pointers, not the difference in their addresses. This is very useful when working with arrays and pointer operations. Below, I will explain this in detail through two examples: a simple array example and a more complex example involving structs.
Simple Array Example
Suppose we have an int array arr and two pointers p and q pointing to elements of the array. Here is a concrete example:
int arr[5] = {10, 20, 30, 40, 50};
int *p = &arr[4]; // Points to the last element of the array
int *q = &arr[1]; // Points to the second element of the array
In this example, pointer p points to the 4th element of the array, and q points to the 1st element. The computation of p - q is as follows:
p - q = (&arr[4] - &arr[1]) = 4 - 1 = 3;
This indicates that there are 3 elements between the position pointed to by q and the position pointed to by p. Therefore, the result of pointer subtraction is the number of elements between the two pointers, not the difference in their memory addresses.
Complex Struct Array Example
To demonstrate more complex pointer operations, we define a struct with two members and create an array of structs.
Defining the Struct and Array
First, we define a struct with two members:
typedef struct {
int id;
float value;
} Data;
Next, we define and initialize a struct array:
Data dataArray[5] = {
{1, 10.5},
{2, 20.0},
{3, 30.2},
{4, 40.8},
{5, 50.3}
};
Pointer Operations
Now, we define two pointers pointing to different elements in the array:
Data *p = &dataArray[3]; // Points to the fourth element of the array
Data *q = &dataArray[0]; // Points to the first element of the array
Computing Pointer Subtraction
We compute the result of subtracting these two pointers:
int difference = p - q;
Explaining the Computation
Both pointers p and q point to elements within the same struct array dataArray, so we can perform subtraction on them. The computation is as follows:
p - q = (&dataArray[3] - &dataArray[0]) = 3 - 0 = 3;
This result indicates that there are 3 Data struct elements between the position pointed to by q and the position pointed to by p.
Complete Code Example
Below is the complete code example:
#include <stdio.h>
typedef struct {
int id;
float value;
} Data;
int main() {
Data dataArray[5] = {
{1, 10.5},
{2, 20.0},
{3, 30.2},
{4, 40.8},
{5, 50.3}
};
Data *p = &dataArray[3]; // Points to the fourth element of the array
Data *q = &dataArray[0]; // Points to the first element of the array
int difference = p - q;
printf("Difference: %d\n", difference); // Outputs 3
return 0;
}
Summary
In C, the result of subtracting two pointers is the number of array elements between the two pointers. This is extremely convenient when working with arrays and pointers. Through the simple array example and the complex struct array example above, you can clearly understand how pointer subtraction is computed and its practical significance. Whether working with arrays of primitive types or arrays of custom structs, pointer subtraction can help us easily determine the number of elements between two pointers.