我正在尝试访问但无法访问的内存。

huangapple go评论54阅读模式
英文:

The memory that i am trying to access and i do not have access to

问题

int main(int argc, char *argv[])
{
    int i;
    int sum = 0;

    if (argc == 1) {
        printf("0\n");
        return (0);
    }
    else {
        for (i = 1; i < argc; i++) {
            if (isdigit(*argv[i]) == 0) {
                printf("Error\n");
                return (1);
            }
            sum += atoi(argv[i]);
        }
    }
    printf("%d\n", sum);
    return (0);
}

当我给它的可执行文件传递任何参数时,上面的代码总是输出“Segmentation fault (core dumped)”,这意味着我试图访问一个我没有访问权限的内存位置。

我认为加上条件(i > argc)应该意味着我确保argc的值允许我访问argv[argc - 1],但我不明白错误的来源,这就是我需要一些帮助来理解的地方。

我期望程序将数字参数相加并输出它们的总和,并且如果存在非数字参数,则打印“Error”一次。

英文:
int main(int argc, char *argv[])
{
    int i;
    int sum = 0;

    if (argc == 1) {
            printf(&quot;0\n&quot;);
            return (0);
    }
    else {
        for (i = 1; i &lt; argc; i++) {
            if (isdigit(argv[i]) == 0) {
                printf(&quot;Error\n&quot;);
                return (1);
            }
            sum += atoi(argv[i]);
        }
    }
    printf(&quot;%d\n&quot;, sum);
    return (0);
}

When I give its executable any argument/s, the above code always outputs "Segmentation fault (core dumped)" which means that I'm trying to access a memory location which I don have access to.

I think that putting the condition (i > argc) should mean that I do make sure that argc is of value allows me to access argv[argc - 1], yet I don't see where the error comes from and this is my where i need some help to understand.

I am expecting the program to add the digits arguments and outputs its sum and to print "Error" once if there a non digit argument

答案1

得分: 2

以下是您发布的代码的修改部分:

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

int main(int argc, char *argv[])
{
    int i;
    int sum = 0;

    if (argc == 1) {
        printf("请键入要求和的数字。示例:myapp 2 4 6");
        return 1;
    }
    else {
        for (i = 1; i < argc; i++) {
            if (!isdigit(argv[i][0])) {
                printf("错误:%s 不是数字!\n", argv[i]);
            }
            else {
                sum += atoi(argv[i]);
            }
        }
    }
    printf("%d\n", sum);
    return (0);
}

以下是对代码的修改概要:

  • 您应该为使用的所有函数包括头文件(例如,#include <stdio.h>)。
  • isdigit() 接受一个字符而不是字符串。
  • 如果 argc 不大于 1,则向用户提供有意义的消息,并返回一个非零结果给系统。
  • 您不一定需要退出程序来处理不良输入:您可以选择以某种方式标记错误并继续执行。这取决于您。
  • 但如果您选择退出,返回一个非零结果给系统。例如,这可以让 shell 脚本确定您的应用程序是否返回了有效的总和。
英文:

Here are a few modifications to the code you posted:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;ctype.h&gt;

int main(int argc, char *argv[])
{
    int i;
    int sum = 0;

    if (argc == 1) {
       printf(&quot;Please enter numbers to sum.  EXAMPLE: myapp 2 4 6&quot;);
       return 1;
    }
    else {
        for (i = 1; i &lt; argc; i++) {
            if (!isdigit(argv[i][0])) {
                printf(&quot;Error: %s isn&#39;t numeric!\n&quot;, argv[i]);
            }
            else {
                sum += atoi(argv[i]);
            }
        }
    }
    printf(&quot;%d\n&quot;, sum);
    return (0);
}
  • You should include headers (e.g. #include <stdio.h>) for all the functions you use.
  • isdigit() accepts a character, not string.
  • If argc isn't > 1, provide a meaningful message to the user, and return a nonzero result to the system.
  • You don't necessarily need to exit for bad input: you might wish to somehow flag the error, and continue. It's up to you.
  • But if you do exit, return a nonzero result to the system. For example, that could allow a shell script to determine whether or not your app returned a valid sum.

huangapple
  • 本文由 发表于 2023年7月10日 10:40:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定