Hank Zhang
Hank Zhang

Reputation: 89

confused about this argv use

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

for (argc--, argv++; argc > 0; argc -= argCount, argv += argCount) {
      argCount = 1;
      switch (argv[0][1]) {
      case 'q':
        testnum = atoi(argv[1]);
        argCount++;
        break;
      default:
        testnum = 1;
        break;
      }
    }
//...............

my question is what does the argv[0][1] mean and the condition in for() confused me i mean for (argc--, argv++; argc > 0; argc -= argCount, argv += argCount)

//thanks guys....**argv[0][1] should be argv[0][1],thats my mistake not the code writers.

Upvotes: 4

Views: 846

Answers (3)

Mike DeSimone
Mike DeSimone

Reputation: 42805

That code doesn't look correct. **argv[0][1] tries to dereference a char.

argv[0][1] would make sense, and means "take the second char of the first char* in argv." IMHO, the code is trying to detect a -q command-line flag (and subsequently setting testnum to the int version of the next argument, blindly assuming it is present), but it's skipping checking for the -, and blindly assuming it's there, and no other arguments would ever have q as a second character.

This code needs to be refactored. Here's one way:

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

    int testnum = 1;
    for (int argi = 1; argi < argc; ++argi) {
        if(argv[argi][0] == '-') {
            switch (argv[argi][1]) {
            case 'q':
                if(argi + 1 == argc || argv[argi + 1][0] == '-') {
                    /* Handle missing argument error. */
                    return 1;
                }
                testnum = atoi(argv[++argi]);
                break;
            default:
               /* Handle unrecognized flag error. */
               return 1;
            }
        }
        else
        {
            /* Handle non-flag parameter. */
        }

    /* Continue with program. */
    return 0;
}

Upvotes: 2

stefan bachert
stefan bachert

Reputation: 9608

This code looks very crazy. I guess you intended to do the following:

int main(int argc,char **argv){
 char** p = argv + 1;  // skipping program name

 while (*p != 0) {     // over all parameters
  testnum = 1;
  if (*p[1] == 'q') {  // skipping - of "-q", not a good idea
    p ++;
    if (*p != 0) {     // may be null, read testnum from parameter,
                       // ?? no check whether this is an integer at all
       testnum = atoi(*p);
    }
  }
}

(not tested, may not compile nor work)

Upvotes: 0

qdii
qdii

Reputation: 12963

argv[0] represents the name of the program as it was invoked on the command line. If you typed ./myprogram --help, then argv[0] would be "./myprogram".

argv[0][1] will be the second character of that string, '/' in the example above.


Let’s see that for (argc--, argv++; argc > 0; argc -= argCount, argv += argCount):

It initializes the loop by doing argc-- then argv++ (argv now points to the second user parameter string) and argc declares an argument less.

The loop is for all arguments argc>0, and at every iteration, the number of treated arguments argCount is taken off the number of all arguments argc. That makes sense.

However switch (**argv[0][1]) doesn’t make any sense, argv[0][1] is a char, as seen before, not a pointer, so it cannot be dereferenced.

Upvotes: 1

Related Questions