No, it's not well defined. It's a constraint violation, requiring a compile-time diagnostic. In practice it's very very likely to work as you expect, but it's not guaranteed to do so, and IMHO it's poor style.
The atoi
function is declared in <stdlib.h>
as:
int atoi(const char *nptr);
You're passing an unsigned char*
argument to a function that expects a char*
argument. The two types are not compatible, and there is no implicit conversion from one to the other. A conforming compiler may issue a warning (that counts as a diagnostic) and then proceed to generate an executable, but the behavior of that executable is undefined.
As of C99, a call to a function with no visible declaration is a constraint violation, so you can't get away with it by omitting the #include <stdlib.h>
.
C does still permit calls to functions with a visible declaration where the declaration is not a prototype (i.e., doesn't define the number of type(s) of the parameters). So, rather than the usual #include <stdlib.h>
, you could add your own declaration:
int atoi();
which would permit calling it with an unsigned char*
argument.
This will almost certainly "work", and it might be possible to construct an argument from the standard that its behavior is well defined. The char
and unsigned char
values of '1'
and '2'
are guaranteed to have the same representation
But it's far easier to add the cast than to prove that it's not necessary -- or, better yet, to define c
as an array of char
rather than as an array of unsigned char
, since it's intended to hold a string.
unsigned char *ptr = strtok(unscharbuff,"-");
This is also a constraint violation. There is no implicit conversion from unsigned char*
to char*
for the first argument in the strtok
call, and there is no implicit conversion from char*
to unsigned char*
for the initialization of ptr
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…