简体   繁体   English

为什么在使用 isdigit function 时出现分段错误?

[英]Why am I getting Segmentation fault while using isdigit function?

I run into segmentation fault while implementing the following program.我在实现以下程序时遇到了分段错误。 Though, the code seems to work partially when I change the variable argv into char *argv, and make subsequent pointers.不过,当我将变量 argv 更改为 char *argv 并创建后续指针时,代码似乎部分工作。

int main(int argc, string argv[] ){

    if(argc == 1) {
        printf("Error");
    }
    else if (argc ==2 )
    {
        if(isdigit(argv[1]))
        {
        int key = atoi (argv[1]);
        printf("Success");

        }
    }
    else  {

       printf("Error");

    }

}

isdigit() expects a character argument, but you're passing a pointer. isdigit()需要一个字符参数,但您传递的是一个指针。 You probably want to loop over all the characters in argv[1] , and make sure they're all digits before calling atoi() .您可能希望遍历argv[1]中的所有字符,并在调用atoi()之前确保它们都是数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM