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?
The C library function long int strtol(const char *str, char **endptr, int base) converts the initial part of the string in str to a long int value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0. Declaration. Following is the declaration for strtol() function. Parameters.
Alias arguments are only passed at the end. Note that f is called at the very end of the alias. The unset -f removes the function definition as the alias is executed so it doesn't hang around afterwards.
If the value is negative, the strtol () function will return LONG_MIN, and the strtoll () function will return LONGLONG_MIN. If no characters are converted, the strtoll () and strtol () functions will set errno to EINVAL and 0 is returned.
The Strict Aliasing rule is enabled by default on optimization levels 2 and beyond, and when one tries to compile a program with aforementioned details the compiler throws warnings, though the program still compiles and can be executed, the output of such a program remains unpredictable.
No. Nothing is accessed via **endptr
in strtol
. Only *endptr
, a completely separate object, is accessed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With