Sam23yo
Sam23yo

Reputation: 21

Command-line argument modification - C programming

int main(int argc, char** argc) {

.....


if(argc != 6 && int argc[1] <30 && int argc[2] <30) {
    }
}

Hey people, I am trying to character limit my command line arguments for main function. This will limit the 2nd and 3rd argument entered to under 30 integers in length. The error i am receiving is "error: subscripted value is neither array nor pointer". In C, is this how I access the index value of each arg, argc0, argc[1] (second arguement) etc. any clarification would be great! thanks.

Upvotes: 0

Views: 636

Answers (2)

NathanFrasier
NathanFrasier

Reputation: 308

Try this:

int main(int argc, char** argv)
{
    //...
    if(argc != 6 && strlen(argv[1]) < 30 && strlen(argv[2]) < 30)
    {
        //do stuff
    }
}

Firstly, you have both of your parameters named argc, that's not right, but ill assume thats just a typo.

Second, argv is a pointer to a string (which is itself a pointer to a char). That means that you cannot compare its value to an integer without some other function call or cast. in this case I assume you mean that you want to limit your argumetns to 30 characters in length.

If you do not want to limit your arguments based on their length but their value you need to use a function call to get the value in the string.

For instance, if you want to limit arguments based on the numerical value of your argument then replace strlen with atoi but then be aware that if a user enters non numeric input you will get strange values.

Upvotes: 2

Nanc
Nanc

Reputation: 464

Change
if(argc != 6 && int argc[1] <30 && int argc[2] <30)
to
if(argc != 6 && strlen (argv[1]) <30 && strlen (argv[2]) <30)

char* argv[] is about parameters.

Upvotes: 4

Related Questions