C++ – How to pass argv[1] to a function that takes 0 arguments

c++command-line-arguments

What exactly do I need to do to get the contents of argv[1] to a function that uses no arguments?

How does this work?

const char *cPtr = argv[1];

and pass it to someFunction() that takes 0 arguments!

Best Solution

If you don't want to pass it as an argument, you'll need to shove it in a global so that the function can access it that way. I know of no other means (other than silly things like writing it to a file in main() and reading it in the function):

static char *x;
static void fn(void) {
    char *y = x;
}
int main (int argc, char *argv[]) {
    x = argv[1];
    fn();
    return 0;
}

This is not how I would do it. Since you need to be able to change fn() to get access to x anyway, I'd rewrite it to pass the parameter:

static void fn(char *x) {
    char *y = x;
}
int main (int argc, char *argv[]) {
    fn(argv[1]);
    return 0;
}

Unless, of course, this is homework, or a brainteaser, or an interview question.