One of the nice things about a low level language. Adding or subtracting to a memory address (technically, you could divide or multiply, but that would be useless and would more than likely segment)

This is usually - scratch that, always - done with some sort of array. For instance, you can loop through an entire zero-terminated array (perfect example: a C string) with just while(*ptr++);. You can even copy a string with just while( *dest++ = *cpy++ );, and in the process totally confuse people.

Let's say you have a pointer, ptr, to an array. So ptr+1 is the memory one unit after ptr, or &ptr[1]. Similarly, ptr-1 is one unit before ptr. Subtraction is also nice if you, say, want to figure out how far an array element is from its start (example: the return of strstr subtracted from its first arg == position in string)

In C, pointer arithmetic is done in sizeof(*ptr) units. Thus, you cannot do arithmetic with a void pointer, as sizeof(void) == 0.
Also, one of the horrible things about low-level languages. Ponter arithmetic is a veritable error-generating machine, because it requires, well, arithmetic, and detailed knowledge about the exact shape of your data structure, things which should be abstracted away as soon as possible. For the same reason, pointer arithmetic can make code very difficult to read. The core problem is that it results in arithmetic errors causing hard-to-track semantic errors, i.e. you don't just get a wrong number somewhere, the program behaves totally different from the way it should, trashing your whole data structure, etc.

That being said, pointer arithmetic is quite necessary and valuable to have in low-level languages, because it allows for very efficient handling of data structures. This is especially important in the field of I/O, where it saves a lot of copying: you can process that byte array directly instead of parsing it into a few dozen objects, additionally incurring a large overhead for object creation.

Log in or registerto write something here or to contact authors.