Valid number how to traverse char inside argv array

Viewed 46

Im trying to do a nested loops to iterate through the characters that I pass through into argv. Lets say I pass multiple args into the command line like 10 30 50 60 I want to do something like this:

void isValid(int argc, char *argv[])
{
    for (int i = 2; i < argc; i++)
    {
        int len = strlen(argv[i]);
        for (int j = 0; j < len; j ++)
        {
            if (strcmp(argv[i][0], "-") == 0)
            {
                printf("- is the first character in index in the first argument");
            }
        }
    }
}

Im getting a warning however that passing argument 1 of ‘strcmp’ makes pointer from integer without a cast. So is this the correct way to iterate through using nested loop? is there a different method that is correct? What am I doing wrong?

1 Answers

C strings are null terminated character arrays. Characters are not just single character strings as they are in other languages. Use single quotes to specify a character literal instead of a string, and this can be compared against the first character of the string:

if (argv[i][0] == '-') {
    printf("- is the first character in index in the first argument");
}
Related