Observe a loop that is used to clear all elements of an array.
#define N_values 5float values[n_values];float *vp;for (vp=&values[0];vp<&values[n_values];) *vp++=0 ;
The For statement uses a relationship test to decide whether to end the loop, which is legal because both VP and pointer constants point to elements in the same data (in fact, this pointer constant points to the memory location behind the last element of the array)! =i is a viable alternative to <
Now consider the following loop:
For (Vp=&values[n_values];vp>&values[0];) *--vp=0;
It is the same as the task G performed by the previous loop, but the array elements are purged in reverse order.
Note: Some people may oppose the writing like *--VP, thinking that the readability is poor, and write down the following wording:
For (Vp=&values[n_values-1];vp>=values[0];) *vp=0;
Warning: After the first element of the array is cleared, the VP value is also subtracted by 1, and the next comparison operation is used to end the loop.
However, the value of the comparison expression Vp>=&values[0] is undefined because the VP is moved outside the bounds of the array. The standard allows a pointer to an array element to be compared to a pointer to the memory location that follows the last n element of the array, but does not allow a pointer to the memory location that precedes the first element of the array.
Relational operations on arrays