6

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?

md5
  • 23,373
  • 3
  • 44
  • 93
  • 1
    A while ago I wrote a blog post on the topic that `restrict` is frustrating as an element of specification because it implicitly refers to the implementation. If a `strtol()` implementation accesses both `**endptr` and `*nptr` then it is undefined to pass `p` and `&p` to it. If it doesn't, then it is not undefined to pass these arguments. The specification implied by `restrict` only makes sense with respect to the very implementation that should be hidden by the specification. http://blog.frama-c.com/index.php?post/2012/08/02/restrict-not-modular – Pascal Cuoq Feb 14 '13 at 18:10

1 Answers1

8

No. Nothing is accessed via **endptr in strtol. Only *endptr, a completely separate object, is accessed.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711