Search code examples
c++arrayspointersconstructorargv

Problems understanding argList constructor


Currently I am analysing the constructor below:

Foam::argList::argList
372 (
373  int& argc,       // argc of main 
374  char**& argv,    // argv of main
375  bool checkArgs,
376  bool checkOpts
377 )
378 :   //Initializing
379  args_(argc), //stringList args_ with size argc                        
380  options_(argc)  //HashTable<string> options_ with size argc
381 {
382  // Check if this run is a parallel run by searching for any parallel option
383  // If found call runPar which might filter argv
384  for (int argI = 0; argI < argc; ++argI)
385  {
386  if (argv[argI][0] == '-') //argv is array of pointers    //HERE 
387  {                         //inline const Foam::string& Foam::argList::operator[]
                               //(const label index) const
                               //{
                               //return args_[index];
                               //}

388  const char *optionName = &argv[argI][1];   //Adressenzuordnung
389 
390  if (validParOptions.found(optionName))
391  {
392  parRunControl_.runPar(argc, argv);
393  break;
394  }
395  }
396  }

I have a question about line 386:

argv should be an array of pointers given as parameter to main(...). With argv[argI] I access the element argI of argv and with [0] the overloaded operator method [] ist called. I really don't get what exactly is happening here, perhaps I am missinterpreting something?


Solution

  • As you mentioned, argv is a char **. argv[x][y] means the y'th element of the x'th pointer. To be more specific, the point of that line is to check if the command line parameter starts with a dash. It has nothing to do with operator overloading, it is a simple acces of an array of pointers.

    Let's see an example:

    ./a.out -bar -foo baz 32 -3
    

    The condition checks for all command line parameters if they begin with a dash. So, it is true for -bar, -foo, -3 but not for baz and 32.