I am trying to convert some chars into numeric type, but some of them may not be null-terminated strings. So are strtol, strtoll, strtod safe for those strings that aren’t null-terminated?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No.
If a character array is not terminated by a null character, then it’s not a string. If any of the
strto*()functions are passed an argument that doesn’t point to a string, the behavior is undefined.Referring to the latest draft of the 2011 ISO C standard:
(emphasis added)
So an argument that doesn’t point to a string is outside the domain of the function.
You’re likely to get away with it if the array contains something like
{ '1', '2', '3', 'x', 'y', 'z' }, since it doesn’t need to scan past thexthat terminates the desired value, but the behavior is explicitly undefined.If you want to use these functions, you should, if necessary, copy your array into another buffer and explicitly null-terminate it yourself.