Here is how strtol
has to be declared according to § 7.22.1.4
from C11 (n1570):
#include <stdlib.h>
long int strtol (const char *restrict nptr,
char **restrict endptr,
int base);
As far as I know, the restrict
keyword means that the object referenced by the lvalue *nptr
will be accessed only with it or a value directly derived from it.
However, a lot of programmers, and even experienced ones, use strtol
in the following way:
#include <stdlib.h>
strtol (p, &p, 10);
In that case, **endptr == **&p == *p == *nptr
, and the behavior is undefined. Is it right?
restrict
is frustrating as an element of specification because it implicitly refers to the implementation. If astrtol()
implementation accesses both**endptr
and*nptr
then it is undefined to passp
and&p
to it. If it doesn't, then it is not undefined to pass these arguments. The specification implied byrestrict
only makes sense with respect to the very implementation that should be hidden by the specification. blog.frama-c.com/index.php?post/2012/08/02/restrict-not-modular – Last