我有自己的 strtol 实现,我认为它可以正常工作。它看起来如下:
long strtol(const char *nPtr, char **endPtr, int base)
{
const char *start;
int number;
long int sum = 0;
int sign = 1;
const char *pos = nPtr;
if (*pos == '\0')
return 0;
start = pos;
while (isspace(*pos))
{
++pos;
}
if (*pos == '-')
{
sign = -1;
++pos;
}
if (*pos == '+')
++pos;
if (base == 16 || base == 8)
{
if (base == 16 && *pos == '0')
++pos;
if (base == 16 && (*pos == 'x' || *pos == 'X'))
++pos;
if (base == 8 && *pos == '0')
++pos;
}
if (base == 0)
{
base = 10;
if (*pos == '0')
{
base = 8;
++pos;
if (*pos == 'x' || *pos == 'X')
{
base = 16;
++pos;
}
}
}
if ((base < 2 || base > 36) && base != 0)
{
errno = EINVAL;
return 0;
}
while (*pos != '\0')
{
number = -1;
if ((int) *pos >= 48 && (int) *pos <= 57)
{
number = (int) *pos - 48;
}
if (isalpha(*pos))
{
number = (int) toupper(*pos) - 55;
}
if (number < base && number != -1)
{
if (sign == -1)
{
if (sum >= ((LONG_MIN + number) / base))
sum = sum * base - number;
else
{
errno = ERANGE;
sum = LONG_MIN;
}
}
else
{
if (sum <= ((LONG_MAX - number) / base))
sum = sum * base + number;
else
{
errno = ERANGE;
sum = LONG_MAX;
}
}
}
else if (base == 16 && number > base
&& (*(pos - 1) == 'x' || *(pos - 1) == 'X'))
{
--pos;
break;
}
else
break;
++pos;
}
if (!isdigit(*(pos - 1)) && !isalpha(*(pos - 1)))
pos = start;
if (endPtr)
*endPtr = (char*) pos;
return sum;
}
但是我对最后一行有疑问:
*endPtr = (char*)pos;
为什么我必须将 pos 强制转换为 (char),endPtr 和 pos 都是指向 char 的指针,在其他情况下,警告会说:Assignment make pointer from integer without cast。感谢帮助