Binary additive operators +
and -
can be used when one argument is a pointer to any complete type (say, T* p
) and the other argument is an integer (say, i
). They implement so called pointer arithmetic.
The compiler assumes that the pointer is pointing to an element of some array (say, T array[N]
). The operation produces a pointer to another element of the array, which is i
elements away from the original element. It is possible to "move" the pointer in either direction, i.e. towards the beginning of the array or towards the end of the array. For example, if p
points to array[3]
, then p + 4
will point to array[7]
.
The operation is only valid when the result points to an existing element of the array or one past the last element of the array, i.e. given the array T array[N]
, it is possible to create pointers to elements from array[0]
to the imaginary element array[N]
. Any attempts to cross these bounds using pointer arithmetic result in undefined behavior.
The type T
has to be complete, meaning that pointer arithmetic cannot be used with void *
pointers, for one example, even though some compilers allow this as an extension (treating void *
pointers as equivalent to char *
pointers).
In addition to the binary additive operators, pointer arithmetic also includes prefix and postfix unary ++
and --
operators (applied to pointers) as well as compound assignment operators +=
and -=
(with pointers on their left-hand side and integers on the right-hand side).
In your case, str + strlen(str)
expression will produce a pointer of char *
type that points to the terminating \0
character in the string str
.