如何检查,以确保你有一个整数调用的atoi()之前? [英] How to check to ensure you have an integer before calling atoi()?
本文介绍了如何检查,以确保你有一个整数调用的atoi()之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望采取一个整数作为命令行参数,但如果用户通过一个非整数的字符串,这将导致堆栈溢出。什么是保证的atoi()将成功的标准方式?
I wish to take an integer as a command line argument, but if the user passes a non-integer string, this will cause a stack overflow. What is the standard way to ensure atoi() will be successful?
推荐答案
您可以使用:
长整型与strtol(为const char * NPTR,焦炭** endptr,INT基地);
然后检查 * endptr!= NPTR
。这意味着字符串至少具有整开始。
您还可以检查* endptr指向终止零,这意味着整个字符串已成功解析。
Then check if *endptr != nptr
. This means that the string at least begins with the integer.
You can also check that *endptr points to terminating zero, which means that the whole string was successfully parsed.
这篇关于如何检查,以确保你有一个整数调用的atoi()之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文