<<< Arrays of pointers example     Index     Implementing swap( ) function >>>

17. Command-line arguments

  • A program gains access to its command-line arguments as follows:

    
    #include <iostream>
    using namespace std;
    
    int main( int argc, char* argv[] )
    {
        int idx;
        for ( idx = 1; i < argc; ++idx ) {
            cout << argv[ idx ] << '\n';
        }
        return 0;
    }
    
    
  • Note that the loop begins at index 1, thus skipping the name of the program.

  • argc is the argument counter

  • argv[ ] is the array of pointers to strings

  • If command line was

        C:\>prog Hello World
    

    then argc is set to 3, and argv is organized like this:

    argv[0] p r o g \0
    argv[1] H e l l o \0
    argv[2] W o r l d \0
    argv[3] NULL

     

<<< Arrays of pointers example     Index     Implementing swap( ) function >>>