template<class IntType>
IntType atoi_unsafe(const char* source)
{
IntType result = IntType();
while (source)
{
auto t = *source;
result *= 10;
result += (*source - 48);
++source;
}
return result;
}
and in main()
I have:
char* number = "14256";
atoi_unsafe<unsigned>(number);
but the condition while (source)
does not seem to recognize that source
has iterated over the entire C string. How should it correctly check for the end of the string?
while(source)
is true until the pointer wraps around to 0, but will probably crash well before that in modern systems. You need to dereference the pointer to find a null byte, while(*source)
.
I hate posting short answers
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