使用 atoi 将命令行参数转换为 int,仅返回输入的第一个数字
我必须使用命令行参数为我的Uni分配设置蛇游戏的地图。我们没有特别告诉我们使用atoi
来帮助将命令行参数从字符串转换为int
,但是我认为atoi
的简单性质会做到这一点。在测试时,我发现它只是占据了第一个数字。
int main(int argc, char *argv[])
{
int isUserInput;
char arg1, arg2, arg3;
arg1 = argv[1][0];
arg2 = argv[2][0];
arg3 = argv[3][0];
isUserInput = checkUserArg(arg1, arg2, arg3);
int checkUserArg(char arg1, char arg2, char arg3)
{
int validation;
int rowMap, colMap, snakeLength;
rowMap = atoi(&arg1);
colMap = atoi(&arg2);
snakeLength = atoi(&arg3);
if ((rowMap < 5) || (colMap < 5) || (snakeLength < 3))
{
validation = FALSE;
}
else if ((rowMap >= 5) || (colMap >= 5) || (snakeLength >= 3))
{
if (snakeLength < colMap)
{
validation = TRUE;
}
else
{
validation = FALSE;
}
}
else
{
validation = FALSE;
}
return validation;
}
用户必须输入3个命令行参数(./文件num1 num2 num2 num3
)。我使用atoi
将字符串命令行参数转换为int
,但是在测试时,我将数字打印回了,它不会仅转换第二个数字,例如1 -9起作用,但是从10个开始的任何内容都只显示了第一个数字。
对为什么这是什么想法? 干杯。
I have to use command line arguments to set up a map for a snake game for my uni assignment. We were not specifically told to use atoi
to help convert the command line argument from string to int
, However I thought the simple nature of atoi
would do the trick. On testing I discovered it is only taking the first digit.
int main(int argc, char *argv[])
{
int isUserInput;
char arg1, arg2, arg3;
arg1 = argv[1][0];
arg2 = argv[2][0];
arg3 = argv[3][0];
isUserInput = checkUserArg(arg1, arg2, arg3);
int checkUserArg(char arg1, char arg2, char arg3)
{
int validation;
int rowMap, colMap, snakeLength;
rowMap = atoi(&arg1);
colMap = atoi(&arg2);
snakeLength = atoi(&arg3);
if ((rowMap < 5) || (colMap < 5) || (snakeLength < 3))
{
validation = FALSE;
}
else if ((rowMap >= 5) || (colMap >= 5) || (snakeLength >= 3))
{
if (snakeLength < colMap)
{
validation = TRUE;
}
else
{
validation = FALSE;
}
}
else
{
validation = FALSE;
}
return validation;
}
User has to enter 3 command line arguments (./file num1 num2 num3
). I used atoi
to convert the string command line arguments to int
, but while testing I printed the numbers back and it won't convert the second digit only the first, e.g 1-9 works, but anything from 10 onwards only shows the first digit.
Any thoughts on why this is?
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码中有多个问题:
atoi
仅使用第一个数字,因为您明确提取第一个数字并将其传递给char
。该函数调用实际上具有&amp; arg1
是单个char> char
的地址,而不是null终结者C字符串的地址。。
checkuserarg
使用atoi
转换参数,如果转换的值超过类型int
,则具有未定义的行为。建议使用strtol
,并允许进行更精细的检查。checkuserarg
应通过指针参数将转换值返回到呼叫者。checkuserarg
的第二个测试是多余的:如果第一个测试为false,则第二个测试中的所有3个比较都是正确的。而不是
true
和false
,您应该使用&lt; stdbool.h&gt;
的定义。。这是修改版本:
There are multiple problems in your code:
atoi
is only using the first digit because you explicitly extract the first digit and pass it as achar
. The function call actually has undefined behavior as&arg1
is the address of a singlechar
, not that of a null terminator C string.checkUserArg
converts the arguments usingatoi
which has undefined behavior if the value converted exceeds the range of typeint
. Usingstrtol
is recommended and allows for finer checks.checkUserArg
should return the converted values to the caller via pointer arguments.the second test in
checkUserArg
is redundant: if the first test is false, then all 3 comparisons in the second test will be true.instead of
TRUE
andFALSE
, you should use definitions from<stdbool.h>
.Here is modified version:
单个字符不是字符串。字符串是一个字符数组,末尾带有空终止符。
你应该这样做:
const
因为我们不应该修改参数。现在这个函数可以直接使用参数调用atoi
。但是
atoi
在设计上是一个损坏的函数,永远不应该使用,因为它没有明确定义的错误处理。直接在 argv 上使用它是危险的。您应该始终使用strtol
而不是atoi
,它是一个更安全、更强大的函数。例子:
A single character is not a string. A string is an array of characters with null termination at the end.
You should do something like this instead:
const
since we shouldn't modify the args. Now this function can callatoi
using the parameters directly.However
atoi
is a broken function by design that should never be used, because it does not have well-defined error handling. Using it directly onargv
is dangerous. You should always usestrtol
instead ofatoi
, it's a safer and more powerful function.Example: