如何将两个数字乘以C中的命令行参数
我正在尝试使用以下代码找到命令行中的参数的两个数字中的多个:
#include <stdio.h>
/**
* main - Entry point for my program
* @argc: This is the number of arguments
* @argv: This is the array of arguments
*
* Return: Zero upon program success
*/
int main(int argc, char *argv[])
{
int i;
int result = 1;
for (i = 1; i < argc; i++)
{
result = result * argv[i];
}
printf("%d\n", result);
return (0);
}
但是,当我尝试编译代码时,我会收到以下错误:
3-mul.c: In function ‘main’:
3-mul.c:19:19: error: invalid operands to binary * (have ‘int’ and ‘char *’)
19 | result = result * argv[i];
| ^ ~~~~~~~
| |
| char *
如何解决此错误?
I am trying to find the multiple of two numbers supplied as arguments in the command line using the following code:
#include <stdio.h>
/**
* main - Entry point for my program
* @argc: This is the number of arguments
* @argv: This is the array of arguments
*
* Return: Zero upon program success
*/
int main(int argc, char *argv[])
{
int i;
int result = 1;
for (i = 1; i < argc; i++)
{
result = result * argv[i];
}
printf("%d\n", result);
return (0);
}
But as I try to compile my code, I get the following error:
3-mul.c: In function ‘main’:
3-mul.c:19:19: error: invalid operands to binary * (have ‘int’ and ‘char *’)
19 | result = result * argv[i];
| ^ ~~~~~~~
| |
| char *
How do I get around this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该行将
行不通,因为
argv [i]
不是整数,而是类型char *
,即指向字符串的指针。用指针乘以整数是没有意义的。您要做的是将指向
argv [1]
的字符串转换为整数,然后将整数乘以result
。这样做的一种简单方法是使用标准库函数strtol
,通过更改并
通过添加
#include&lt; stdlib.h&gt;
到文件开始。如果转换失败,则功能
strtol
将简单地返回0
,导致您的最终结果也为0
。另外,如果用户输入的数字如此之大,以至于无法表示为long
,则功能strtol
将简单地返回最大的代表数字。如果不是您想要的,那么您可能想打印一个错误消息:对于命令行参数,
该程序具有以下输出:
对于命令行参数,
此程序具有以下输出:
对于命令行参数
此程序具有以下输出:
对于命令行参数,
该程序具有以下输出:
此程序仍然不完美,因为如上所示,它正在接受命令行参数
5ABC 作为数字
5
的有效表示。如果您也希望该参数也被拒绝,那么该程序将必须执行进一步的验证测试。The line
will not work, because
argv[i]
is not an integer, but it is of typechar *
, i.e. a pointer to a string. It does not make sense to multiply an integer with a pointer.What you want to do is to convert the string pointed to by
argv[1]
to an integer, and then multiply that integer withresult
. A simple way to do this would be to use the standard library functionstrtol
, by changing the lineto
and by adding
#include <stdlib.h>
to the start of the file.If the conversion fails, then the function
strtol
will simply return0
, causing your final result to also be0
. Also, if the user enters a number that is so large that it is not representable as along
, then the functionstrtol
will simply return the largest representable number. If that is not what you want, then you probably want to print an error message instead:For the command-line arguments
this program has the following output:
For the command-line arguments
this program has the following output:
For the command-line arguments
this program has the following output:
For the command-line arguments
this program has the following output:
This program is still not perfect, though, because, as shown above, it is accepting the command-line argument
5abc
as a valid representation for the number5
. If you want that argument to be rejected too, then the program would have to perform further validation tests.