使用isdigit()和argc时C中的分段错误

log*_*ick 1 c cs50

我遇到以下问题:分段错误(核心转储).

我查看了StackOverflow上的其他问题,但没有看到我问题的正确答案.这是我的代码:

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

int main(int argc, string argv[])
{
if (argc != 2)
{
    printf("missing command-line argument\n");
    return 1;
}

for (int i = 0; i < argc; i++)
{
    printf("Made it inside");
    if (isdigit(argv[i]) == 0)
    {
        return 1;
    }
}
string plain_text = get_string("plaintext: ");
int key = atoi(argv[1]); //function to convert a string to int. 


for (int i = 0, n = strlen(plain_text); i < n; i++)
{
    int c = (int) plain_text[i];
    c += key;
    printf("%c", c);
}
printf("\n");

return 0;

}
Run Code Online (Sandbox Code Playgroud)

错误来自"if(isdigit(argv [i])== 0)".

编辑:

这是解决方案,谢谢大家!

 for (int i = 0; i < strlen(argv[1]); i++)
{
    if (isdigit(argv[1][i]) == 0)
    {
        return 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

dan*_*705 5

您传递string这实际上是一个char *对isdigit.您需要传递字符串的第一个字符

isdigit(*argv[i][0])
Run Code Online (Sandbox Code Playgroud)