How can I pass main's *argv[] to a function?

前端 未结 3 1260
你的背包
你的背包 2021-02-05 08:19

I have a program that can accept command-line arguments and I want to access the arguments, entered by the user, from a function. How can I pass the *argv[], from <

相关标签:
3条回答
  • 2021-02-05 08:39

    Just write a function such as

    void parse_cmdline(int argc, char *argv[])
    {
        // whatever you want
    }
    

    and call that in main as parse_cmdline(argc, argv). No magic involved.

    In fact, you don't really need to pass argc, since the final member of argv is guaranteed to be a null pointer. But since you have argc, you might as well pass it.

    If the function need not know about the program name, you can also decide to call it as

    parse_cmdline(argc - 1, argv + 1);
    
    0 讨论(0)
  • 2021-02-05 08:45
    SomeResultType ParseArgs( size_t count, char**  args ) {
        // parse 'em
    }
    

    Or...

    SomeResultType ParseArgs( size_t count, char*  args[] ) {
        // parse 'em
    }
    

    And then...

    int main( int size_t argc, char* argv[] ) {
        ParseArgs( argv );
    }
    
    0 讨论(0)
  • 2021-02-05 08:49

    Just pass argc and argv to your function.

    0 讨论(0)
提交回复
热议问题