how to point to char *argv[] in main?

arrays, c, c++, pointers, program-entry-point

Solution

In C++, usually* the best way to handle `argv` is this:

int main(int argc, char **argv)
{
  std::vector<std::string> args(argv, argv + argc);
}

Now you have `args`, a correctly constructed `std::vector` holding each element of `argv` as a `std::string`. No ugly C-style `const char*` in sight.

Then you can just pass this vector or some of its elements as you need.

* It carries the memory & time overhead of dynamically allocating one copy of each `argv` string. But for the vast majority of programs, command-line handling is not performance-critical and the increased maintainability and robustness is well worth it.

Problem

In c++ I've got a main function with ``` int argc, char * argv[] ``` I need to access the data in `argv[]` (i.e the arguments) in another function. I am going to declare a global variable, a pointer to the `char **argv`. How do I do this?

Original source