在我的程序中,我有一个字符数组(char data[]),我必须从中提取数据并将其存储在整数变量(值)中。为此,我在程序中使用了 atoi。问题是我有时会在字符数组(数据)中收到 null 。如果我在这个值上使用 atoi,我会在整数变量中得到值 0 而不是 (null)
真正的功能相当大,我无法在这里发布完整的代码。通过下面提到的功能,我希望你们能够了解我在这里所做的事情。
Get_Data(char data[])
{
int value;
value = atoi(p);
}
我最近读到我们应该使用 strtol 而不是 atoi。我也尝试过。但我的变量值仍然为 0。
我想知道应该使用哪个函数才能获得整数变量值(null)?
这里所说的 null,是指空字符数组
请提供针对该问题的具体解决方案。在上面的函数中,如果 data 得到空 char 值,我如何确保我的 int 变量值也得到空值而不是零??
最佳答案
atoi 返回零
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.
此外,您要求返回 NULL
也是不可能的,因为您在 int
中分配这些值,而 int
不能保存 NULL
。
关于c++ - atoi 中的 null 将值转换为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24862360/