5

I am clear about the difference between char* argv[] and char** argv (as explained, for instance, in this question).

But what kind of type is char** argv[] ? A pointer to a pointer to a pointer of chars? And what would that be, when used to pass arguments to the program?

For the record, the type occurs in the following declaration of a C++ library I am trying to interface to Python:

/** Initialize the Stage library. Stage will parse the argument
  array looking for parameters in the conventional way. */
  void Init( int* argc, char** argv[] );
Community
  • 1
  • 1
stefano
  • 769
  • 2
  • 10
  • 24
  • 8
    Hopefully it's a typo – M.M Mar 10 '15 at 20:24
  • 1
    Same as a `char ***argv` in that context FWIW, but I'm guessing from `main`, `argv` points somewhere else after you pass `&argv`. The function signature is odd, though, because the `[]` conveys that the argument should be an array, and I can't imagine what else to give it besides a single element `argv` – chris Mar 10 '15 at 20:24
  • 1
    FYI: http://www.cdecl.org/ translates it to _declare argv as array of pointer to pointer to char_ – Motti Mar 10 '15 at 20:26
  • @Motti: Which is wrong when argv is a function parameter. – Benjamin Lindley Mar 10 '15 at 20:30
  • Sounds like you stumbled upon a [three-star programmer](http://c2.com/cgi/wiki?ThreeStarProgrammer). – Daniel Kamil Kozar Mar 10 '15 at 20:45

3 Answers3

4

If main definition is main(int argc, char * argv[]) and you want to send argv via pointer, you will do Init( &argc, &argv ). And the corresponding parameter can be char ** argv[].

i486
  • 6,491
  • 4
  • 24
  • 41
  • @Deduplicator just curious, is it well defined behaviour to modify `argc` and `argv`? – vsoftco Mar 10 '15 at 20:29
  • @i486: Of course! I should have looked at main first. It's exactly as you said. – stefano Mar 10 '15 at 20:34
  • @Deduplicator: stage is a robotic simulation envronment which may include several robotic controllers. The Init function passes the command line arguments to all the controllers in the simulation. – stefano Mar 10 '15 at 20:39
2

I assume that this Init function will make changes to the argc and argv that main has received, so it needs pointers to them.

If the library uses some program arguments, the Init function can handle them, and then remove those arguments from the argument list before it is processed by the rest of the program.

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
1

char** argv[] is an array of char**, IOW an array of pointers to pointers to chars, or more likely an array of pointers to null-terminated character strings, eg:

char *str1 = "...";
char *str2 = "....";

char** argv[2];
argv[0] = &str1;
argv[1] = &str2;

However, if argv is coming from main() itself, then char** is a typo, it must be char* instead:

char* argv[]

But if argv is coming from some library instead, it very well could be wrapping the original argv array from main() inside its own array, in which case char**[] might make sense so it can point at the original strings and not have to copy them.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770