如何将两个数字乘以C中的命令行参数

发布于 2025-02-10 05:13:29 字数 811 浏览 0 评论 0原文

我正在尝试使用以下代码找到命令行中的参数的两个数字中的多个:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

述情 2025-02-17 05:13:29

该行将

result = result * argv[i];

行不通,因为argv [i]不是整数,而是类型char *,即指向字符串的指针。用指针乘以整数是没有意义的。

您要做的是将指向argv [1]的字符串转换为整数,然后将整数乘以result。这样做的一种简单方法是使用标准库函数 strtol ,通过更改

result = result * argv[i];

result = result * strtol( argv[i], NULL, 10 );

通过添加#include&lt; stdlib.h&gt;到文件开始。

如果转换失败,则功能strtol将简单地返回0,导致您的最终结果也为0。另外,如果用户输入的数字如此之大,以至于无法表示为long,则功能strtol将简单地返回最大的代表数字。如果不是您想要的,那么您可能想打印一个错误消息:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main( int argc, char *argv[] )
{
    long result = 1;

    for ( int i = 1; i < argc; i++ )
    {
        long num;
        char *p;

        //attempt to convert argv[i] to integer
        errno = 0;
        num = strtol( argv[i], &p, 10 );

        //verify that conversion was successful
        if ( p == argv[i] )
        {
            fprintf( stderr, "Conversion of argv[%d] failed!\n", i );
            exit( EXIT_FAILURE );
        }

        //check for range error
        if ( errno == ERANGE )
        {
            fprintf( stderr, "Argument of argv[%d] out of range!\n", i );
            exit( EXIT_FAILURE );
        }

        //conversion was successful and the range was ok, so we
        //can now use the converted number
        result = result * num;
    }

    //print the sum
    printf( "%ld\n", result );

    return EXIT_SUCCESS;
}

对于命令行参数,

3 abc

该程序具有以下输出:

Conversion of argv[2] failed!

对于命令行参数,

3 500000000000000000000

此程序具有以下输出:

Argument of argv[2] out of range!

对于命令行参数

3 5

此程序具有以下输出:

15

对于命令行参数,

3 5abc

该程序具有以下输出:

15

此程序仍然不完美,因为如上所示,它正在接受命令行参数5ABC 作为数字5的有效表示。如果您也希望该参数也被拒绝,那么该程序将必须执行进一步的验证测试。

The line

result = result * argv[i];

will not work, because argv[i] is not an integer, but it is of type char *, 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 with result. A simple way to do this would be to use the standard library function strtol, by changing the line

result = result * argv[i];

to

result = result * strtol( argv[i], NULL, 10 );

and by adding #include <stdlib.h> to the start of the file.

If the conversion fails, then the function strtol will simply return 0, causing your final result to also be 0. Also, if the user enters a number that is so large that it is not representable as a long, then the function strtol will simply return the largest representable number. If that is not what you want, then you probably want to print an error message instead:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main( int argc, char *argv[] )
{
    long result = 1;

    for ( int i = 1; i < argc; i++ )
    {
        long num;
        char *p;

        //attempt to convert argv[i] to integer
        errno = 0;
        num = strtol( argv[i], &p, 10 );

        //verify that conversion was successful
        if ( p == argv[i] )
        {
            fprintf( stderr, "Conversion of argv[%d] failed!\n", i );
            exit( EXIT_FAILURE );
        }

        //check for range error
        if ( errno == ERANGE )
        {
            fprintf( stderr, "Argument of argv[%d] out of range!\n", i );
            exit( EXIT_FAILURE );
        }

        //conversion was successful and the range was ok, so we
        //can now use the converted number
        result = result * num;
    }

    //print the sum
    printf( "%ld\n", result );

    return EXIT_SUCCESS;
}

For the command-line arguments

3 abc

this program has the following output:

Conversion of argv[2] failed!

For the command-line arguments

3 500000000000000000000

this program has the following output:

Argument of argv[2] out of range!

For the command-line arguments

3 5

this program has the following output:

15

For the command-line arguments

3 5abc

this program has the following output:

15

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 number 5. If you want that argument to be rejected too, then the program would have to perform further validation tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文