Hi, i am the beginner of c language, may i know what is the negative effect for atoi function ? cause in some condition , it may cause segmentation error!!!
Thank you.
Printable View
Hi, i am the beginner of c language, may i know what is the negative effect for atoi function ? cause in some condition , it may cause segmentation error!!!
Thank you.
>> Hi, i am the beginner of c language, may i know what is the negative effect for atoi function ? cause in some condition , it may cause segmentation error!!!
The only way you're going to get a seg fault is if you pass it an invalid pointer, which shouldn't even happen in the first place. The only real shortcoming of atoi is that it doesn't tell you if the input string was invalid or not. But you could probably use strtol or sprintf if you wanted that information, though.
sscanf(), maybe? ;-)Quote:
But you could probably use strtol or sprintf if you wanted that information, though.
Now I haven't done C in a while but wouldn't a Scanset only guarantee input from the range specified and thereby avoid such an error? also sscanf does..... what again?
The best functions to use are strtol(), strtoul() and strtod().
The disadvantage of sscanf() and similar functions is that they can't report numeric overflow (like scanning 123456789012345 into a 32-bit int).
atoi() never causes a segmentation fault.
YOU cause a segmentation fault, by passing a bad pointer to atoi().