C、为什么会出现分段错误(核心转储)

发布于 2025-01-19 02:15:40 字数 506 浏览 2 评论 0 原文

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

int main(int argc, string argv[])
{
    int KEY;
    if(isdigit(argv[1]))
    {
        KEY = (int)argv[1];
    }
    else
    {
        printf("Usage: ./caesar key");
    }
    printf("\n");
    printf("%i\n",argc);
}

时找不到问题

当我输入以下在此处输入图像描述

如何修复它?

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

int main(int argc, string argv[])
{
    int KEY;
    if(isdigit(argv[1]))
    {
        KEY = (int)argv[1];
    }
    else
    {
        printf("Usage: ./caesar key");
    }
    printf("\n");
    printf("%i\n",argc);
}

I can't find out the problem when I type the following

enter image description here

How do I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

百变从容 2025-01-26 02:15:40

这不是一种具有各种语法糖功能的高级语言。

重要提示:

  • const char *int 的转换不能仅通过类型转换来完成。
  • isdigit() 仅适用于 int (单个字符),不适用于 const char * 它会导致 未定义的行为
  • argc 是指针数组的长度 argv
  • string 不是 main() 函数参数的有效类型,它应该是 int main(int argc, char const **argv) { }
  • 必须检查 argv[1] 是否存在
  • 一旦你打印了用法,你的程序应该退出
  • 任何错误消息都应打印在stderr
  • 退出应用程序时必须使用return EXIT_SUCCESS;,它是在头文件stdlib.h中定义的,或者使用return EXIT_FAILURE; 如果出现问题。

最终代码

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

int main(int argc, char const **argv)
{
    if(argc == 1)
    {
        fprintf(stderr, "Usage: %s <KEY>", argv[0]);
        return EXIT_FAILURE;
    }

    for(size_t i = 0; argv[1][i]; i++)
    {
        if(!isdigit(argv[1][i]))
        {
            fprintf(stderr, "expected only integer value\n");
            return EXIT_FAILURE;
        }
    }

    // we don't need to check whether `argv[1]` is all digit or not at this point 
    int KEY = strtod(argv[1], NULL);
    printf("argc = %d\n", argc);
    printf("KEY = %d\n", KEY);
    return EXIT_SUCCESS;
}

This is not a high-level language with various syntax sugar features.

Important Notes:

  • Conversion of const char * to int is not done by just type-casting it.
  • isdigit() works only for int (single characters), not const char * it will cause Undefined Behavior.
  • argc is the length of pointer array argv
  • string is not a valid type for main() function arguments, it should be int main(int argc, char const **argv) { }
  • Must check whether argv[1] even exists or not
  • Once you print the usage your program should exit
  • Any error message should be printed on stderr
  • Must Use return EXIT_SUCCESS;, which is defined in the header file stdlib.h, when exiting the application, or use return EXIT_FAILURE; if something goes wrong.

Final Code

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

int main(int argc, char const **argv)
{
    if(argc == 1)
    {
        fprintf(stderr, "Usage: %s <KEY>", argv[0]);
        return EXIT_FAILURE;
    }

    for(size_t i = 0; argv[1][i]; i++)
    {
        if(!isdigit(argv[1][i]))
        {
            fprintf(stderr, "expected only integer value\n");
            return EXIT_FAILURE;
        }
    }

    // we don't need to check whether `argv[1]` is all digit or not at this point 
    int KEY = strtod(argv[1], NULL);
    printf("argc = %d\n", argc);
    printf("KEY = %d\n", KEY);
    return EXIT_SUCCESS;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文