ptr
is a pointer to the interior of a null terminated string. So given "34EF"
it ends up pointing to the character 'E'
and the string starting at that address is "EF"
.
A four-character C string like p = "34EF"
actually contains five strings in one. The string p
is "34EF"
. The string p+1
is "4EF"
; the string p+2
is "EF"
; p+3
is "F"
and p+4
is the empty string ""
. In this case p+4
points to the null terminator byte after the F
.
Speaking of the empty string, if the input to strtol
consists only of valid characters making up the numeric token, then ptr
should point to an empty string.
If you want to disallow trailing junk, you can test for this. That is, even if a valid number parses out, if *ptr
is not 0, then the input has trailing junk. In some cases, it is good to reject that: "Dear user, 10Zdf is not a number; please enter a number!"