Modifying the Value of a Pointer
Learn how to modify the value of a pointer and understand its effects on modification.
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:
Press + to interact
import std.stdio;void main() {int variable = 20;int *ptr = &variable;writeln(ptr);++ptr;writeln(ptr);--ptr;writeln(ptr);ptr += 2;writeln(ptr);ptr -= 2;writeln(ptr);writeln(ptr + 3);writeln(ptr - 3);}
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; //
...