Modifying the Value of a Pointer
Explore how to modify pointer values in D through increments and decrements that adjust the memory address safely. Understand the automatic scaling of pointer arithmetic by variable size and identify cases that lead to undefined behavior. Gain practical knowledge of using pointers with arrays and slices to maintain valid memory references.
We'll cover the following...
Pointer value modification
The values of pointers can be incremented or decremented, and they can be used in addition and subtraction:
Different from their arithmetic counterparts, these operations do not modify the actual value by the specified amount. Rather, the value of the pointer (an address) gets modified so that it now points at the variable that is a certain number of variables beyond the current one. The amount of the increment or the decrement specifies how many variables away should the pointer point at.
For example, incrementing the value of a pointer makes it point at the next variable:
++ptr; ...